'C++' std::inner_product

토스트·2025년 5월 2일

'C++' std::numeric

목록 보기
5/5

inner_product

template<class InputIt1, class InputIt2, class T>
T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init); // 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); // constexpr since C++20

: 두 개 범위의 내적(inner product)을 계산하고 그 값을 반환합니다.

  • first1 : 첫 번째 범위의 첫 번째 요소를 가리키는 입력 반복자
  • last1 : 첫 번째 범위의 마지막 번째 요소의 뒤를 가리키는 입력 반복자
  • first2 : 두 번째 범위의 첫 번째 요소를 가리키는 입력 반복자 (두 범위는 같아야 합니다.)
  • init : 초기 값
  • op1 : 첫 번째 이항 연산자(기본적으로 std::plus<>)
  • op2 : 두 번째 이항 연산자(기본적으로 std::multiplies<>)

<example>

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int main() {
    vector<int> vec1 = { 10, 15, 5, 2, 4 };
    vector<int> vec2 = { 5, 7, 9, 11, 13 };
    
    cout << inner_product(vec1.begin(), vec1.end(), vec2.begin(), 0);

    return 0;
}

결과값

계산 과정
0 + 10 * 5 + 15 * 7 + 5 * 9 + 2 * 11 + 4 * 13 = 274

0개의 댓글