Advertisement
cd62131

C++ Accumulate and Transform

Feb 28th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <numeric>
  6. #include <vector>
  7. using namespace std;
  8. int main() {
  9.   vector<int> v = {1, 2, 3, 4, 5};
  10.   for (int& e: v) cout << e << ' '; cout << endl;
  11.   cout << "Accumulate: " << accumulate(v.begin(), v.end(), 0) << endl;
  12.  
  13.   vector<int> u = {10, 20, 30, 40, 50};
  14.   vector<int> x;
  15.   x.resize(v.size());
  16.   transform(v.begin(), v.end(), u.begin(), x.begin(), plus<int>());
  17.   for (int& e: x) cout << e << ' '; cout << endl;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement