/* vectorlab.cpp: A study of STL's vector container * * ADD DOCUMENTATION HERE (as per the Programming Style Sheet) *************************************************************************/ #include #include using namespace std; template ostream & operator<<(ostream & out, const vector & v) { for (int i = 0; i < v.size(); i++) out << v[i] << " "; return out; } int main() { // Declare 6 vectors v1, v2, v3, v4, v5, v6 to illustrate the // various types of declarations (and constructors) vector v1; vector v2(2); int numInts; cout << "Enter capacity of v3: "; cin >> numInts; vector v3(numInts); vector v4(3, 99); // The preceding declaration should work, but it may not in some // versions of some compilers. The following is a work-around: // vector v4(3); // for (int i = 0; i < 3; i++) v4[i] = 99; //--- End of work-around int a[] = {1, 4, 9, 16, 25}; vector v5(a, a+5); vector v6; //--- 1 --- Add: // Statements to display the capacity and size of each vector // and whether it is empty //--- 2 --- Add: // Statements to display the maximum capacity of a vector //--- 3 --- Add: // Statements to see the effect of the reserve() member function //--- 4 --- Add: // Output statements of the form cout << vector-variable << endl; // to display the contents of each vector //--- 5 --- Add: // Statements to append 11 to v2 and then output v2's size and contents // append 22 to v2 and then output v2's size and contents // append 33 to v2 and then output v2's size and contents // remove the last element of v2 and then output v2's size // and contents //--- 6 --- Statements to investigate how capacities grow // Add statements to append 111 to v1 and then output v1's capacity, size, // and contents //--- 7 --- Statements to investigate how capacities grow // Add statements to append 222, 333, 444, and 555 to v1 and output // v1's capacity, size, and contents after each value is appended //--- 8 --- Statements to investigate how capacities grow // Remove the comment delimiters from the following: /* int oldCapacity = v1.capacity(); for (int i = v1.size() + 1; i <= 2500; i++) { v1.push_back(999); if (v1.capacity() == v1.size()) cout << "\n*** For i = " << i << " v1 is full\n"; if (v1.capacity() > oldCapacity) { cout << "For i = " << i << " capacity increased from " << oldCapacity << " to " << v1.capacity() << endl; oldCapacity = v1.capacity(); } } */ //--- 9 --- Statements to see if element type affects how capacities grow // Add: // A declaration of an empty vector v0; // A loop like the preceding but with v1 replaced by v0 // // Then change double to char and run it again. //--- 10 --- Statements to see how initial capacity affects // how capacities grow // Uncomment the following line: // cout << "Initial capacity of v4 is " << v4.capacity() << endl; // Add a loop like that in 9 but output changes in v4's capacity //--- 11 --- Statements to access the ends of a vector // Uncomment the following line: // cout << "Original contents of v5: " << v5 << endl; // Add statements to: // Output the first and last elements of v5 // Change the first element to 77 and the last element to 88 // Output the contents of v5 //--- 12 --- Statements to demonstrate correct and incorrect // use of the subscript operator // Add statements that try using the subscript operator to: // change a value in v5 // append a value to v5 // append a value to v2 // append a value to empty vector v6 //--- 13 --- Add statements that: // assign v2 to v3 // check if they are equal // check if v5 is less than v2 // swap contents of v5 and v2 // check if v5 is less than v2 }