[ Web Proxy ]
URL:
Viewing: http://ja.cppreference.com/cpp/algorithm/inner_product [Back]  [Original]

std::inner_product - cppreference.com
cppreference.com

std::inner_product

: cppreference.com
 
C++
(C++20)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
 
(C++20)
: std::sortable, std::projected, ...
: std::ranges::copy, std::ranges::sort, ...
(C++17)
(C++11)(C++11)(C++11)
(C++17)
(C++11)
()
(C++11)
/
(C++11)
(C++17)

C
 
<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>
(1)
template< class InputIt1, class InputIt2, class T > T inner_product( InputIt1 first1, InputIt1 last1, InputIt2 first2, T init );
(C++20)
template< class InputIt1, class InputIt2, class T > constexpr T inner_product( InputIt1 first1, InputIt1 last1, InputIt2 first2, T init );
(C++20)
(2)
template<class InputIt1, class InputIt2, class T, class BinaryOperation1, class BinaryOperation2> T inner_product( InputIt1 first1, InputIt1 last1, InputIt2 first2, T init, BinaryOperation1 op1, BinaryOperation2 op2 );
(C++20)
template<class InputIt1, class InputIt2, class T, class BinaryOperation1, class BinaryOperation2> constexpr T inner_product( InputIt1 first1, InputIt1 last1, InputIt2 first2, T init, BinaryOperation1 op1, BinaryOperation2 op2 );
(C++20)

[first1, last1) first2 () map/reduce

1) acc init last1

acc = acc + *first1 * *first2 acc = acc + *(first1+1) * *(first2+1)

(C++20)

acc = std::move(acc) + *first1 * *first2 acc = std::move(acc) + *(first1+1) * *(first2+1)

(C++20)
+ * 2
2) acc init last1

acc = op1(acc, op2(*first1, *first2)) acc = op1(acc, op2(*(first1+1), *(first2+1)))

(C++20)

acc = op1(std::move(acc), op2(*first1, *first2)) acc = op1(std::move(acc), op2(*(first1+1), *(first2+1)))

(C++20)

op1 op2

(C++11)

op1 op2

(C++11)

first1, last1 - 1
first2 - 2
init -
op1 - op2

Ret fun(const Type1 &a, const Type2 &b);

const &
Type1 Type2 T Type3 Ret T

op2 - 1

Ret fun(const Type1 &a, const Type2 &b);

const &
Type1 Type2 InputIt1 InputIt2 Type1 Type2 Ret Type3

-
InputIt1, InputIt2 LegacyInputIterator
-
ForwardIt1, ForwardIt2 LegacyForwardIterator
-
T CopyAssignable CopyConstructible

acc

1
template<class InputIt1, class InputIt2, class T>
constexpr // since C++20
T inner_product(InputIt1 first1, InputIt1 last1,
                InputIt2 first2, T init)
{
    while (first1 != last1) {
         init = std::move(init) + *first1 * *first2; // std::move since C++20
         ++first1;
         ++first2;
    }
    return init;
}
2
template<class InputIt1, class InputIt2,
         class T,
         class BinaryOperation1, class BinaryOperation2>
constexpr // since C++20
T inner_product(InputIt1 first1, InputIt1 last1,
                InputIt2 first2, T init,
                BinaryOperation1 op1
                BinaryOperation2 op2)
{
    while (first1 != last1) {
         init = op1(std::move(init), op2(*first1, *first2)); // std::move since C++20
         ++first1;
         ++first2;
    }
    return init;
}

std::transform_reduce op1 op2 std::inner_product

#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<>(), std::equal_to<>());
    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

reduce
() [edit]

() [edit]

() [edit]

Web Proxy Viewer  |  New URL  |  Original Page