Advertisement
cwchen

[C] Modifying a vector in-place

Aug 27th, 2017
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.45 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. using std::vector;
  5. using std::cout;
  6. using std::endl;
  7.  
  8. int main() {
  9.     vector<int> vec {1, 2, 3};
  10.  
  11.     // Modify vec in-place!
  12.     for (auto ptr = vec.begin(); ptr != vec.end(); ptr++) {
  13.         *ptr = (*ptr) * (*ptr);
  14.     }
  15.  
  16.     // Print out the data of vec in console
  17.     for (auto ptr = vec.begin(); ptr != vec.end(); ptr++) {
  18.         cout << *ptr << " ";
  19.     }
  20.     cout << endl;
  21.  
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement