std::inner_product
| 헤더 <numeric>
|
||
template< class InputIt1, class InputIt2, class T >
T inner_product( InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init );
|
에 정의됨 (1) | (constexpr since C++20) |
template< class InputIt1, class InputIt2, class T,
class BinaryOp1, class BinaryOp2 >
T inner_product( InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init,
BinaryOp1 op1, BinaryOp2 op2 );
|
(2) | (constexpr since C++20) |
내적(즉, 곱의 합)을 계산하거나 범위 [first1, last1)와 std::distance(first1, last1)에서 시작하는 first2개 요소 범위에 대해 순서가 있는 맵/리듀스 연산을 수행합니다.
acc(타입 T)를 초기값 init으로 초기화한 다음 범위 acc = acc + (*i1) * (*i2)(C++20까지)acc = std::move(acc) + (*i1) * (*i2)(C++20부터)의 각 반복자 i1에 대해 순서대로 그리고 [first1, last1)에서 시작하는 범위의 해당 반복자 i2에 대해 식 first2으로 수정합니다. + 및 *의 내장 의미에 대해 두 범위의 내적을 계산합니다.acc(타입 T)를 초기값 init으로 초기화한 다음 범위 acc = op1(acc, op2(*i1, *i2))(C++20까지)acc = op1(std::move(acc), op2(*i1, *i2))(C++20부터)의 각 반복자 i1에 대해 순서대로 그리고 [first1, last1)에서 시작하는 범위의 해당 반복자 i2에 대해 식 first2으로 수정합니다.last2이 std::distance(first1, last1)번째 first2의 다음 반복자라고 할 때, 다음 조건 중 하나라도 만족하면 동작이 정의되지 않습니다:
T이 CopyConstructible이 아닌 경우.T이 CopyAssignable이 아닌 경우.op1또는op2이[first1,last1)또는[first2,last2)의 요소를 수정하는 경우.op1또는op2이[first1,last1]또는[first2,last2]의 반복자나 하위 범위를 무효화하는 경우.
매개변수
| first1, last1 | - | 요소의 범위를 정의하는 반복자 쌍 |
| first2 | - | 두 번째 요소 범위의 시작 |
| init | - | 곱의 합의 초기값 |
| op1 | - | 적용될 이진 연산 함수 객체입니다. 이 "합" 함수는 op2에서 반환된 값과 누산기의 현재 값을 받아 누산기에 저장할 새 값을 생성합니다.함수의 시그니처는 다음과 같아야 합니다:
시그니처에 |
| op2 | - | 적용될 이진 연산 함수 객체입니다. 이 "곱" 함수는 각 범위에서 하나의 값을 가져와 새 값을 생성합니다. 함수의 시그니처는 다음과 같아야 합니다:
시그니처에 |
| 타입 요구 사항 | ||
-InputIt1, InputIt2은 LegacyInputIterator의 요구 사항을 충족해야 합니다.
| ||
반환값
acc 모든 수정 후의 값.
가능한 구현
| inner_product (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;
}
|
| inner_product (2) |
template<class InputIt1, class InputIt2, class T,
class BinaryOp1, class BinaryOp2>
constexpr // since C++20
T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init,
BinaryOp1 op1, BinaryOp2 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 <functional>
#include <iostream>
#include <numeric>
#include <vector>
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
결함 보고서
다음 동작 변경 결함 보고서는 이전에 발행된 C++ 표준에 소급하여 적용되었습니다.
| DR | 적용 대상 | 발행 시 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 242 | C++98 | op1 및 op2은 부작용이 있을 수 없음
|
해당 범위를 수정할 수 없음 |
참고 항목
(C++17) |
호출 가능 객체를 적용한 후 순서 없이 리듀스합니다 (함수 템플릿) |
| 요소 범위를 합산하거나 폴드합니다 (함수 템플릿) | |
| 요소 범위의 부분 합을 계산합니다 (함수 템플릿) |