kolioi

35. Find the Sum of All Numbers in Vector C++

Dec 9th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <numeric>
  4.  
  5. int main()
  6. {
  7.     using namespace std;
  8.  
  9.     int vector_size;
  10.     cin >> vector_size;
  11.  
  12.     vector<int> v;
  13.     for (int i = 0; i < vector_size; ++i)
  14.     {
  15.         int n;
  16.         cin >> n;
  17.         v.push_back(n);
  18.     }
  19.  
  20.     int sum = accumulate(v.begin(), v.end(), 0);
  21.  
  22.     cout << sum << endl;
  23.  
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment