Advertisement
Guest User

C++: vector size vs. vector capacity

a guest
Dec 17th, 2019
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. //////// vector size vs. vector capacity
  7. //////// Size: the number of items currently in the vector
  8. //////// Capacity: how many items can be fit in the vector before it is "full". Once full, adding new items will result in a new, larger block of memory being allocated and the existing items being copied to it
  9. //////// source: https://stackoverflow.com/a/6296965/9868445
  10.  
  11. int main(){
  12.  
  13.   vector <int> a;
  14.  
  15.   a.push_back(1);
  16.   a.push_back(2);
  17.   a.push_back(3);
  18.   cout << a.size() << endl;
  19.   cout << a.capacity() << endl;
  20.   a.pop_back();
  21.   a.pop_back();
  22.   a.pop_back();
  23.   cout << a.size() << endl;
  24.   cout << a.capacity() << endl;
  25.   a.push_back(1);
  26.   a.push_back(2);
  27.   a.push_back(3);
  28.   a.push_back(4);
  29.   cout << a.size() << endl;
  30.   cout << a.capacity() << endl;
  31.   a.push_back(5);
  32.   cout << a.size() << endl;
  33.   cout << a.capacity() << endl;
  34.  
  35.   return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement