std::inner_product
![]() |
ÐÑа ÑÑÑаниÑа бÑла пеÑеведена авÑомаÑиÑеÑки Ñ Ð°Ð½Ð³Ð»Ð¸Ð¹Ñкой веÑÑии вики иÑполÑзÑÑ ÐеÑеводÑик Google.
ÐеÑевод Ð¼Ð¾Ð¶ÐµÑ ÑодеÑжаÑÑ Ð¾Ñибки и ÑÑÑаннÑе ÑоÑмÑлиÑовки. ÐаведиÑе кÑÑÑÐ¾Ñ Ð½Ð° ÑекÑÑ, ÑÑÐ¾Ð±Ñ ÑвидеÑÑ Ð¾Ñигинал. ÐÑ Ð¼Ð¾Ð¶ÐµÑе помоÑÑ Ð² иÑпÑавлении оÑибок и ÑлÑÑÑении пеÑевода. ÐÐ»Ñ Ð¸Ð½ÑÑÑÑкÑий пеÑейдиÑе по ÑÑÑлке. |
Defined in header <numeric>
|
||
template< class InputIt1, class InputIt2, class T > T inner_product( InputIt1 first1, InputIt1 last1, |
(1) | |
template< class InputIt1, |
(2) | |
[first1, last1)
диапазона и дÑÑгой диапазон наÑало в first2
. ÐеÑвÑй ваÑÐ¸Ð°Ð½Ñ Ð¸ÑполÑзÑеÑÑÑ operator*
Ð´Ð»Ñ Ð²ÑÑиÑÐ»ÐµÐ½Ð¸Ñ Ð¿ÑÐ¾Ð¸Ð·Ð²ÐµÐ´ÐµÐ½Ð¸Ñ ÑлеменÑов Ð¿Ð°Ñ Ð¸ operator+
подвеÑÑи иÑоги пÑодÑкÑов, вÑоÑой ваÑÐ¸Ð°Ð½Ñ Ð¸ÑполÑзÑÐµÑ op2
и op1
Ð´Ð»Ñ ÑеÑÐµÐ½Ð¸Ñ ÑÑиÑ
Ð·Ð°Ð´Ð°Ñ ÑооÑвеÑÑÑвенно.[first1, last1)
and another range beginning at first2
. The first version uses operator*
to compute product of the element pairs and operator+
to sum up the products, the second version uses op2
and op1
for these tasks respectively.ÐÑ Ð¼Ð¾Ð¶ÐµÑе пÑовеÑиÑÑ Ð¸ иÑпÑавиÑÑ Ð¿ÐµÑевод. ÐÐ»Ñ Ð¸Ð½ÑÑÑÑкÑий ÑÑлкниÑе ÑÑда.
СодеÑжание |
[пÑавиÑÑ] ÐаÑамеÑÑÑ
first1, last1 | â | ÐеÑвÑй диапазон ÑлеменÑов
ÐÑигинал: the first range of elements ТекÑÑ Ð±Ñл пеÑеведÑн авÑомаÑиÑеÑки иÑполÑзÑÑ ÐеÑеводÑик Google. ÐÑ Ð¼Ð¾Ð¶ÐµÑе пÑовеÑиÑÑ Ð¸ иÑпÑавиÑÑ Ð¿ÐµÑевод. ÐÐ»Ñ Ð¸Ð½ÑÑÑÑкÑий ÑÑлкниÑе ÑÑда. |
first2 | â | РнаÑале вÑоÑого ÑÑда ÑлеменÑов
ÐÑигинал: the beginning of the second range of elements ТекÑÑ Ð±Ñл пеÑеведÑн авÑомаÑиÑеÑки иÑполÑзÑÑ ÐеÑеводÑик Google. ÐÑ Ð¼Ð¾Ð¶ÐµÑе пÑовеÑиÑÑ Ð¸ иÑпÑавиÑÑ Ð¿ÐµÑевод. ÐÐ»Ñ Ð¸Ð½ÑÑÑÑкÑий ÑÑлкниÑе ÑÑда. |
value | â | ÐаÑалÑное знаÑение ÑÑÐ¼Ð¼Ñ Ð¿Ñоизведений
ÐÑигинал: initial value of the sum of the products ТекÑÑ Ð±Ñл пеÑеведÑн авÑомаÑиÑеÑки иÑполÑзÑÑ ÐеÑеводÑик Google. ÐÑ Ð¼Ð¾Ð¶ÐµÑе пÑовеÑиÑÑ Ð¸ иÑпÑавиÑÑ Ð¿ÐµÑевод. ÐÐ»Ñ Ð¸Ð½ÑÑÑÑкÑий ÑÑлкниÑе ÑÑда. |
op1 | â | binary operation function object that will be applied. ÐÑа ÑÑнкÑÐ¸Ñ Ð¿ÑÐ¸Ð½Ð¸Ð¼Ð°ÐµÑ Ð·Ð½Ð°Ñение, возвÑаÑенное op2 и ÑекÑÑее знаÑение аккÑмÑлÑÑоÑа и пÑÐ¾Ð¸Ð·Ð²Ð¾Ð´Ð¸Ñ Ð½Ð¾Ð²Ð¾Ðµ знаÑение ÑоÑ
ÑанÑеÑÑÑ Ð² аккÑмÑлÑÑоÑе . ÐÑигинал: This function takes a value returned by op2 and the current value of the accumulator and produces a new value to be stored in the accumulator. ТекÑÑ Ð±Ñл пеÑеведÑн авÑомаÑиÑеÑки иÑполÑзÑÑ ÐеÑеводÑик Google. ÐÑ Ð¼Ð¾Ð¶ÐµÑе пÑовеÑиÑÑ Ð¸ иÑпÑавиÑÑ Ð¿ÐµÑевод. ÐÐ»Ñ Ð¸Ð½ÑÑÑÑкÑий ÑÑлкниÑе ÑÑда. The signature of the function should be equivalent to the following: Ret fun(const Type1 &a, const Type2 &b); The signature does not need to have const &. |
op2 | â | binary operation function object that will be applied. ÐÑа ÑÑнкÑÐ¸Ñ Ð¿ÑÐ¸Ð½Ð¸Ð¼Ð°ÐµÑ Ð¾Ð´Ð½Ð¾ знаÑение из каждого диапазона и ÑÐ¾Ð·Ð´Ð°ÐµÑ Ð½Ð¾Ð²Ð¾Ðµ знаÑение . ÐÑигинал: This function takes one value from each range and produces a new value. ТекÑÑ Ð±Ñл пеÑеведÑн авÑомаÑиÑеÑки иÑполÑзÑÑ ÐеÑеводÑик Google. ÐÑ Ð¼Ð¾Ð¶ÐµÑе пÑовеÑиÑÑ Ð¸ иÑпÑавиÑÑ Ð¿ÐµÑевод. ÐÐ»Ñ Ð¸Ð½ÑÑÑÑкÑий ÑÑлкниÑе ÑÑда. The signature of the function should be equivalent to the following: Ret fun(const Type1 &a, const Type2 &b); The signature does not need to have const &. |
ТÑÐµÐ±Ð¾Ð²Ð°Ð½Ð¸Ñ Ðº Ñипам | ||
-InputIt1, InputIt2 должен ÑооÑвеÑÑÑвоваÑÑ ÑÑебованиÑм InputIterator .
| ||
-T должен ÑооÑвеÑÑÑвоваÑÑ ÑÑебованиÑм CopyAssignable и CopyConstructible .
|
[пÑавиÑÑ] ÐозвÑаÑаемое знаÑение
ÐÑ Ð¼Ð¾Ð¶ÐµÑе пÑовеÑиÑÑ Ð¸ иÑпÑавиÑÑ Ð¿ÐµÑевод. ÐÐ»Ñ Ð¸Ð½ÑÑÑÑкÑий ÑÑлкниÑе ÑÑда.
[пÑавиÑÑ] ÐÐ¾Ð·Ð¼Ð¾Ð¶Ð½Ð°Ñ ÑеализаÑиÑ
ÐеÑвÑй ваÑÐ¸Ð°Ð½Ñ |
---|
template<class InputIt1, class InputIt2, class T> T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T value) { while (first1 != last1) { value = value + *first1 * *first2; ++first1; ++first2; } return value; } |
ÐÑоÑой ваÑÐ¸Ð°Ð½Ñ |
template<class InputIt1, class InputIt2, class T, class BinaryOperation1, class BinaryOperation2> T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T value, BinaryOperation1 op1 BinaryOperation2 op2) { while (first1 != last1) { value = op1(value, op2(*first1, *first2)); ++first1; ++first2; } return value; } |
[пÑавиÑÑ] ÐÑимеÑ
#include <numeric> #include <iostream> #include <vector> #include <functional> int main() { std::vector<int> a{0, 1, 2, 3, 4}; std::vector<int> b{5, 4, 2, 3, 1}; int r1 = std::inner_product(a.begin(), a.end(), b.begin(), 0); std::cout << "Inner product of a and b: " << r1 << '\n'; int r2 = std::inner_product(a.begin(), a.end(), b.begin(), 0, std::plus<int>(), std::equal_to<int>()); std::cout << "Number of pairwise matches between a and b: " << r2 << '\n'; }
ÐÑвод:
Inner product of a and b: 21 Number of pairwise matches between a and b: 2
[пÑавиÑÑ] См. Ñакже
ÑÑммиÑÑÐµÑ Ð´Ð¸Ð°Ð¿Ð°Ð·Ð¾Ð½ ÑлеменÑов (Ñаблон ÑÑнкÑии) | |
вÑÑиÑлÑÐµÑ ÑаÑÑиÑнÑÑ ÑÑÐ¼Ð¼Ñ ÑÑда ÑлеменÑов ÐÑигинал: computes the partial sum of a range of elements ТекÑÑ Ð±Ñл пеÑеведÑн авÑомаÑиÑеÑки иÑполÑзÑÑ ÐеÑеводÑик Google. ÐÑ Ð¼Ð¾Ð¶ÐµÑе пÑовеÑиÑÑ Ð¸ иÑпÑавиÑÑ Ð¿ÐµÑевод. ÐÐ»Ñ Ð¸Ð½ÑÑÑÑкÑий ÑÑлкниÑе ÑÑда. (Ñаблон ÑÑнкÑии) |