2

I've just started with C++'s standard library and the first thing I started with is std::vector. I've a bit of confusion with the capacity() in a vector. I know that after each push_back(), the capacity of the vector changes in exponential powers, but in the below output the capacity remains the same value sometimes, even after insertions. Can someone kindly explain to me the internal working?

#include<iostream>
#include<vector>

using namespace std;

int main(){
    vector<int> v;
    int capacity=v.capacity();
    cout<<"Capacity before push_back(): "<<capacity<<endl;
    for(int i=0;i<10;i++){
        v.push_back(i);
        cout<<"Capacity: "<<v.capacity()<<endl;
        
    }
    for(auto j=v.begin();j!=v.end();j++){
        cout<<*j<<endl;
    }
     
    cout<<"Size of vector: "<<v.size()<<endl;
    cout<<"Final Capacity of vector: "<<v.capacity()<<endl;
    
    return 0;
}

OUTPUT:

Capacity before push_back(): 0
Capacity: 1
Capacity: 2
Capacity: 4
Capacity: 4
Capacity: 8
Capacity: 8
Capacity: 8
Capacity: 8
Capacity: 16
Capacity: 16
0
1
2
3
4
5
6
7
8
9
Size of vector: 10
Final Capacity of vector: 16
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    The vector only allocates new capacity if the new size would be greater than the current capacity. – Miles Budnek Jul 17 '20 at 17:33
  • 2
    As a beginner in C++, you can simply forget about the capacity. Just act as if the concept didn't exist. The size() is important, but the capacity is not a useful concept until you deal with advanced or memory-intensive programs – Jeffrey Jul 17 '20 at 17:36
  • 2
    "capacity" is how many chairs there are at a table. "size" is how many people are using those chairs. push_back seats a person at the table (increasing size), but if there's not enough chairs, someone has to go buy a bunch more chairs, and _then_ the person can sit. – Mooing Duck Jul 17 '20 at 17:58
  • Does this answer your question? [size vs capacity of a vector?](https://stackoverflow.com/questions/6296945/size-vs-capacity-of-a-vector) – Ulrich Eckhardt Jul 17 '20 at 18:06

3 Answers3

10

I know that after each push_back() the capacity of the vector changes in exponential powers but in the above OUTPUT the capacity is still remaining same sometimes even after insertion.

When capacity is greater than the size after insertion, the capacity doesn't need to, and is guaranteed not to change.

This strategy allows sequential push back to have constant complexity (amortised).

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

From Vector: [emphasis added]

The storage of the vector is handled automatically, being expanded and contracted as needed. Vectors usually occupy more space than static arrays, because more memory is allocated to handle future growth. This way a vector does not need to reallocate each time an element is inserted, but only when the additional memory is exhausted. The total amount of allocated memory can be queried using capacity() function. Extra memory can be returned to the system via a call to shrink_to_fit(). (since C++11)

H.S.
  • 11,654
  • 2
  • 15
  • 32
1

I know that after each push_back(), the capacity of the vector changes in exponential powers, but in the below output the capacity remains the same value sometimes, even after insertions.

This is where your misunderstanding lies. The vector's CAPACITY does not increase on every insert, its SIZE increases instead. The CAPACITY grows when an insertion causes the SIZE to increase beyond the current CAPACITY, or if you explicitly request the CAPACITY to increase by calling the reserve() method.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770