Skip to content

Instantly share code, notes, and snippets.

@monhime
Last active January 18, 2021 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monhime/486071b573298398edad3aeb643d4b55 to your computer and use it in GitHub Desktop.
Save monhime/486071b573298398edad3aeb643d4b55 to your computer and use it in GitHub Desktop.
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