자료구조 & 알고리즘/기타
내적 계산
diesuki4
2023. 4. 2. 19:24
transform() 함수와 accumulate() 함수를 사용해 직접 구현하면 다음과 같다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
void main()
{
vector<int> A = {1, 2, 3};
vector<int> B = {4, 5, 6};
transform(A.begin(), A.end(), B.begin(), A.begin(), ::multiplies<int>());
cout << accumulate(A.begin(), A.end(), 0);
}
출력
32
<numeric> 헤더를 포함하고 inner_product() 함수를 사용하면 바로 구할 수 있다.
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
void main()
{
vector<int> A = {1, 2, 3};
vector<int> B = {4, 5, 6};
cout << inner_product(A.begin(), A.end(), B.begin(), 0);
}
출력
32