A vector
, and some other containers, have several kinds of sizes defined:
.size()
: how many elements the vector currently contains
.capacity()
: how many elements it could contain before reallocation
.max_size()
: the maximum theoretical size
Think of .capacity()
as the volume of a box, as opposed to .size()
,
which is how much the box is holding now.
vector<int> v; cout << "size=" << v.size() << '\n' << "capacity=" << v.capacity() << '\n' << "max_size=" << v.max_size() << '\n';
size=0 capacity=0 max_size=4611686018427387903
vector<int> v; for (int i=0; i<17; i++) { v.push_back(42); cout << "size=" << v.size() << ' ' << "capacity=" << v.capacity() << '\n'; }
size=1 capacity=1 size=2 capacity=2 size=3 capacity=4 size=4 capacity=4 size=5 capacity=8 size=6 capacity=8 size=7 capacity=8 size=8 capacity=8 size=9 capacity=16 size=10 capacity=16 size=11 capacity=16 size=12 capacity=16 size=13 capacity=16 size=14 capacity=16 size=15 capacity=16 size=16 capacity=16 size=17 capacity=32
.reserve()
and .resize()
.reserve(
n)
: change the allocated size of this
container. However, you can’t allocate less than your current .size()
.
This changes .capacity()
. This does not change .size()
.
.resize(
n)
: change the size of this container.
If it had more than n elements before, we lose some. If it had
fewer than n elements, add default-constructed elements.
This changes .size()
. It may change .capacity()
.
.reserve()
vector<int> v; v.reserve(7); for (int i=0; i<17; i++) { v.push_back(42); cout << "size=" << v.size() << ' ' << "capacity=" << v.capacity() << '\n'; }
size=1 capacity=7 size=2 capacity=7 size=3 capacity=7 size=4 capacity=7 size=5 capacity=7 size=6 capacity=7 size=7 capacity=7 size=8 capacity=14 size=9 capacity=14 size=10 capacity=14 size=11 capacity=14 size=12 capacity=14 size=13 capacity=14 size=14 capacity=14 size=15 capacity=28 size=16 capacity=28 size=17 capacity=28
.resize()
vector<int> v; v.resize(7); for (int i=0; i<16; i++) { v.push_back(42); cout << "size=" << v.size() << ' ' << "capacity=" << v.capacity() << '\n'; }
size=8 capacity=14 size=9 capacity=14 size=10 capacity=14 size=11 capacity=14 size=12 capacity=14 size=13 capacity=14 size=14 capacity=14 size=15 capacity=28 size=16 capacity=28 size=17 capacity=28 size=18 capacity=28 size=19 capacity=28 size=20 capacity=28 size=21 capacity=28 size=22 capacity=28 size=23 capacity=28
string s; for (int i=0; i<17; i++) { s += 'x'; cout << "size=" << s.size() << ' ' << "capacity=" << s.capacity() << '\n'; }
size=1 capacity=15 size=2 capacity=15 size=3 capacity=15 size=4 capacity=15 size=5 capacity=15 size=6 capacity=15 size=7 capacity=15 size=8 capacity=15 size=9 capacity=15 size=10 capacity=15 size=11 capacity=15 size=12 capacity=15 size=13 capacity=15 size=14 capacity=15 size=15 capacity=15 size=16 capacity=30 size=17 capacity=30
set<int> s; for (int i=0; i<17; i++) { s.insert(i); cout << "size=" << s.size() << ' ' << "capacity=" << s.capacity() << '\n'; }
c.cc:5: error: 'class std::set<int>' has no member named 'capacity'