1: //vector10.cpp
3: #include <iostream>
4: #include <vector>
5: using namespace std;
7: int main()
8: {
9: cout << "\nThis program illustrates how one vector can be "
10: "assigned to another vector\nof the same component type, "
11: "using the assignment operator (=), and also\nillustrates "
12: "the use of the assign() member function for assigning "
13: "values\nto a vector.";
14: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
16: cout << "\nFirst we demonstrate use of the assignment operator.";
17: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
19: int a1[] = {1, 2, 3, 4, 5};
20: vector<int> v1(a1, a1+5);
21: cout << "\nFor v1 we have ...";
22: cout << "\nSize = " << v1.size() << " Capacity = " << v1.capacity();
23: cout << "\nContents: ";
24: for(vector<int>::size_type i=0; i<v1.size(); i++)
25: cout << v1.at(i) << " ";
26: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
28: int a2[] = {1, 2, 3, 4, 5, 6, 7};
29: vector<int> v2(a2, a2+7);
30: cout << "\nFor v2 we have ...";
31: cout << "\nSize = " << v2.size() << " Capacity = " << v2.capacity();
32: cout << "\nContents: ";
33: for(vector<int>::size_type i=0; i<v2.size(); i++)
34: cout << v2.at(i) << " ";
35: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
37: int a3[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
38: vector<int> v3(a3, a3+10);
39: cout << "\nFor v3 we have ...";
40: cout << "\nSize = " << v3.size() << " Capacity = " << v3.capacity();
41: cout << "\nContents: ";
42: for(vector<int>::size_type i=0; i<v3.size(); i++)
43: cout << v3.at(i) << " ";
44: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
46: v2 = v1;
47: cout << "\nNow we assign v1 to v2 (v2 = v1). "
48: "\nThen for v2 we have ...";
49: cout << "\nSize = " << v2.size() << " Capacity = " << v2.capacity();
50: cout << "\nContents: ";
51: for(vector<int>::size_type i=0; i<v2.size(); i++)
52: cout << v2.at(i) << " ";
53: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
55: v2 = v3;
56: cout << "\nNow we assign v3 to v2 (v2 = v3). "
57: "\nThen for v2 we have ...";
58: cout << "\nSize = " << v2.size() << " Capacity = " << v2.capacity();
59: cout << "\nContents: ";
60: for(vector<int>::size_type i=0; i<v2.size(); i++)
61: cout << v2.at(i) << " ";
62: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
64: cout << "\nNow we demonstrate use of the assign() member function.";
65: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
67: int a4[] = {2, 4, 6, 8};
68: vector<int> v4(a4, a4+4);
69: cout << "\nFor v4 we have ...";
70: cout << "\nSize = " << v4.size() << " Capacity = " << v4.capacity();
71: cout << "\nContents: ";
72: for(vector<int>::size_type i=0; i<v4.size(); i++)
73: cout << v4.at(i) << " ";
74: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
76: v4.assign(3, 10);
77: cout << "\nNow we perform v4.assign(3, 10). "
78: "\nThen for v4 we have ...";
79: cout << "\nSize = " << v4.size() << " Capacity = " << v4.capacity();
80: cout << "\nContents: ";
81: for(vector<int>::size_type i=0; i<v4.size(); i++)
82: cout << v4.at(i) << " ";
83: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
85: v4.assign(6, 12);
86: cout << "\nNow we perform v4.assign(6, 12). "
87: "\nThen for v4 we have ...";
88: cout << "\nSize = " << v4.size() << " Capacity = " << v4.capacity();
89: cout << "\nContents: ";
90: for(vector<int>::size_type i=0; i<v4.size(); i++)
91: cout << v4.at(i) << " ";
92: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
94: int a5[] = {1, 3, 5, 7, 9};
95: vector<int> v5(a5, a5+5);
96: cout << "\nFor v5 we have ...";
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: int a6[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
104: vector<int> v6(a6, a6+10);
105: cout << "\nFor v6 we have ...";
106: cout << "\nSize = " << v6.size() << " Capacity = " << v6.capacity();
107: cout << "\nContents: ";
108: for(vector<int>::size_type i=0; i<v6.size(); i++)
109: cout << v6.at(i) << " ";
110: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
112: v5.assign(v6.begin()+2, v6.begin()+5);
113: cout << "\nNow we perform v5.assign(v6.begin()+2, v6.begin()+5). "
114: "\nThen for v5 we have ...";
115: cout << "\nSize = " << v5.size() << " Capacity = " << v5.capacity();
116: cout << "\nContents: ";
117: for(vector<int>::size_type i=0; i<v5.size(); i++)
118: cout << v5.at(i) << " ";
119: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
121: v5.assign(v6.begin()+1, v6.begin()+8);
122: cout << "\nNow we perform v5.assign(v6.begin()+1, v6.begin()+8). "
123: "\nThen for v5 we have ...";
124: cout << "\nSize = " << v5.size() << " Capacity = " << v5.capacity();
125: cout << "\nContents: ";
126: for(vector<int>::size_type i=0; i<v5.size(); i++)
127: cout << v5.at(i) << " ";
128: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
129: }