CS253: Software Development with C++

Spring 2018

Container Sizes

See this page as a slide show

CS253 Container Sizes

Container sizes

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

Think of .capacity() as the volume of a box, as opposed to .size(), which is how much the box is holding now.

Example

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

Example

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()

Example with .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

Example with .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

Strings, too

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

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'

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-24T16:51

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building