#include <iostream> #include <vector> using namespace std; int main() { vector<int> a; cout << "a.size(): " << a.size() << " a.capacity(): " << a.capacity() << endl; for (int i = 0; i < 10; i++) { a.push_back(i); cout << "a.size(): " << a.size() << " a.capacity(): " << a.capacity() << endl; } cout << endl; vector<int> b; b.reserve(10); for (int i = 0; i < 10; i++) { b.push_back(i); cout << "b.size(): " << b.size() << " b.capacity(): " << b.capacity() << endl; } b.push_back(11); cout << "b.size(): " << b.size() << " b.capacity(): " << b.capacity() << endl; cout << endl; b.reserve(15); cout << "after b.reserve(15):" << endl; cout << "b.size(): " << b.size() << " b.capacity(): " << b.capacity() << endl; b.resize(5); cout << "after b.resize(5):" << endl; cout << "b.size(): " << b.size() << " b.capacity(): " << b.capacity() << endl; return 0; }
a.size(): 0 a.capacity(): 0 a.size(): 1 a.capacity(): 1 a.size(): 2 a.capacity(): 2 a.size(): 3 a.capacity(): 4 a.size(): 4 a.capacity(): 4 a.size(): 5 a.capacity(): 8 a.size(): 6 a.capacity(): 8 a.size(): 7 a.capacity(): 8 a.size(): 8 a.capacity(): 8 a.size(): 9 a.capacity(): 16 a.size(): 10 a.capacity(): 16 b.size(): 1 b.capacity(): 10 b.size(): 2 b.capacity(): 10 b.size(): 3 b.capacity(): 10 b.size(): 4 b.capacity(): 10 b.size(): 5 b.capacity(): 10 b.size(): 6 b.capacity(): 10 b.size(): 7 b.capacity(): 10 b.size(): 8 b.capacity(): 10 b.size(): 9 b.capacity(): 10 b.size(): 10 b.capacity(): 10 b.size(): 11 b.capacity(): 20 after b.reserve(15): b.size(): 11 b.capacity(): 20 after b.resize(5): b.size(): 5 b.capacity(): 20
(全文完)html