Skip to content

Instantly share code, notes, and snippets.

@monhime

monhime/accum_pair.cpp

Last active Jan 18, 2021
Embed
What would you like to do?
pair型のベクタのfirst, secondそれぞれの和
#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