CS253: Software Development with C++

Fall 2019

Container Sizes

Show Lecture.ContainerSizes as a slide show.

CS253 Container Sizes

Container sizes

A vector, and some other containers, have several kinds of sizes defined:

.size() is how much the box is holding now, .capacity() is the volume of a box, and .max_size() is the maximum size a box could be before it undergoes gravitational collapse.

Example

vector<int> v;
cout << "size=" << v.size() << '\n'
     << "capacity=" << v.capacity() << '\n'
     << "max_size=" << v.max_size() << '\n'
     << "max_size=" << float(v.max_size()) << '\n';
size=0
capacity=0
max_size=4611686018427387903
max_size=4.61169e+18
vector<char> v(42);
cout << "size=" << v.size() << '\n'
     << "capacity=" << v.capacity() << '\n'
     << "max_size=" << v.max_size() << '\n';
size=42
capacity=42
max_size=18446744073709551615

FYI, 264 ≈ 18.4×1018 (eighteen quintillion)

Dynamic Resizing

vector<int> v;
for (int i=0; i<18; 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
size=18 capacity=32

.reserve() and .resize()

Example with .reserve()

vector<int> v;
v.reserve(7);
for (int i=0; i<18; 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
size=18 capacity=28

Example with .resize()

vector<int> v;
v.resize(7);
for (int i=0; i<18; 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
size=24 capacity=28
size=25 capacity=28

Strings, too

string s;
for (int i=0; i<18; 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
size=18 capacity=30

How about a set?

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'