1: //vector15.cpp 2: //Based on the Feb 13, 2006 InformIT article by Danny Kalev: 3: //"Controlling a Container's Capacity" 5: #include <iostream> 6: #include <string> 7: #include <vector> 8: using namespace std; 10: int main() 11: { 12: cout << "\nThis program illustrates how to shrink the capacity " 13: "of a vector object, and\nthereby simulate \"trim\" and " 14: "\"reset\" member functions, which do not currently\nexist " 15: "in the vector class interface."; 16: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 18: cout << "\nFirst we illustrate capacity behavior when vector " 19: "objects are assigned."; 20: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 22: vector<int> v1(5, 5); 23: v1.reserve(7); 24: cout << "\nFor v1 we have:"; 25: cout << "\nSize = " << v1.size() << " Capacity = " << v1.capacity(); 26: cout << "\nContents: "; 27: for(vector<int>::size_type i=0; i<v1.size(); i++) 28: cout << v1.at(i) << " "; 29: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 31: vector<int> v2(10, 10); 32: v2.reserve(12); 33: cout << "\nFor v2 we have:"; 34: cout << "\nSize = " << v2.size() << " Capacity = " << v2.capacity(); 35: cout << "\nContents: "; 36: for(vector<int>::size_type i=0; i<v2.size(); i++) 37: cout << v2.at(i) << " "; 38: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 40: vector<int> v3(15, 15); 41: v3.reserve(17); 42: cout << "\nFor v3 we have:"; 43: cout << "\nSize = " << v3.size() << " Capacity = " << v3.capacity(); 44: cout << "\nContents: "; 45: for(vector<int>::size_type i=0; i<v3.size(); i++) 46: cout << v3.at(i) << " "; 47: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 49: cout << "\nNow we perform v1 = v2 and v3 = v2 and then redisplay " 50: "both v1 and v3."; 51: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 52: v1 = v2; 53: v3 = v2; 54: cout << "\nFor v1 we have:"; 55: cout << "\nSize = " << v1.size() << " Capacity = " << v1.capacity(); 56: cout << "\nContents: "; 57: for(vector<int>::size_type i=0; i<v1.size(); i++) 58: cout << v1.at(i) << " "; 59: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 60: cout << "\nFor v3 we have:"; 61: cout << "\nSize = " << v3.size() << " Capacity = " << v3.capacity(); 62: cout << "\nContents: "; 63: for(vector<int>::size_type i=0; i<v3.size(); i++) 64: cout << v3.at(i) << " "; 65: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 67: cout << "\nNext we illustrate capacity behavior when vector " 68: "objects are copied."; 69: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 70: cout << "\nSo ... we create v4 as a copy of v3."; 71: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 72: vector<int> v4(v3); 73: cout << "\nFor v4 we have:"; 74: cout << "\nSize = " << v4.size() << " Capacity = " << v4.capacity(); 75: cout << "\nContents: "; 76: for(vector<int>::size_type i=0; i<v4.size(); i++) 77: cout << v4.at(i) << " "; 78: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 80: cout << "\nNow we show how to \"trim\" the capacity of a vector " 81: "down to match its size."; 82: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 84: vector<int> v5(5, 5); 85: v5.reserve(10); 86: cout << "\nFor v5 we have:"; 87: cout << "\nSize = " << v5.size() << " Capacity = " << v5.capacity(); 88: cout << "\nContents: "; 89: for(vector<int>::size_type i=0; i<v5.size(); i++) 90: cout << v5.at(i) << " "; 91: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 93: cout << "\nNow we use the \"self-swapping\" idom " 94: "vector<int>(v5).swap(v5)\nto \"trim\" the capacity of v5."; 95: vector<int>(v5).swap(v5); 96: cout << "\nAfter this, we have for v5:"; 97: cout << "\nSize = " << v5.size() << " Capacity = " << v5.capacity(); 98: cout << "\nContents: "; 99: for(vector<int>::size_type i=0; i<v5.size(); i++) 100: cout << v5.at(i) << " "; 101: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 103: cout << "\nFinally we show how to \"reset\" a vector so that both " 104: "\nits size and its capacity are reduced to zero."; 105: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 107: vector<int> v6(5, 5); 108: v6.reserve(10); 109: cout << "\nFor v6 we have:"; 110: cout << "\nSize = " << v6.size() << " Capacity = " << v6.capacity(); 111: cout << "\nContents: "; 112: for(vector<int>::size_type i=0; i<v6.size(); i++) 113: cout << v6.at(i) << " "; 114: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 116: cout << "\nNow we use \"swapping-with-an-empty-temporary\" idom " 117: "vector<int>().swap(v6)\nto \"reset\" v6 to an empty vector " 118: "of capacity zero."; 119: vector<int>().swap(v6); 120: cout << "\nAfter this, we have for v6:"; 121: cout << "\nSize = " << v6.size() << " Capacity = " << v6.capacity(); 122: cout << "\nContents: "; 123: for(vector<int>::size_type i=0; i<v6.size(); i++) 124: cout << v6.at(i) << " "; 125: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 126: }