Make Unreal REAL.
article thumbnail

 

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
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그