Last active
January 18, 2021 13:56
-
-
Save monhime/486071b573298398edad3aeb643d4b55 to your computer and use it in GitHub Desktop.
pair型のベクタのfirst, secondそれぞれの和
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <numeric> | |
using namespace std; | |
int main(){ | |
vector<int> A = {1, 2, 3, 3, 2}; | |
int sum = accumulate(A.begin(), A.end(), 0, [](int init, int v){ return init + v; }); | |
cout << sum << "\n"; | |
using Pii = pair<int, int>; | |
vector<Pii> A_p = {{1, 2}, {2, 2}, {3, 5}, {3, 5}, {2, 1}}; | |
Pii sum_p = accumulate(A_p.begin(), A_p.end(), Pii(0, 0), [](Pii init, Pii v){ return Pii(init.first + v.first, init.second + v.second); }); | |
cout << sum_p.first << " " << sum_p.second << "\n"; | |
auto fx = [](Pii init, Pii v) -> Pii { return Pii(init.first + v.first, init.second + v.second); }; | |
Pii sum_p2 = accumulate(A_p.begin(), A_p.end(), Pii(0, 0), fx); | |
cout << sum_p2.first << " " << sum_p2.second << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment