forked from wadehuber/codeexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectors.cpp
More file actions
165 lines (145 loc) · 4.85 KB
/
Copy pathvectors.cpp
File metadata and controls
165 lines (145 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
template<typename T> void printVector(vector<T> vec, const string name);
int main() {
vector<int> v1(10); // Create a vector with a capacity of 10
vector<int> v2 = {10, 20, 30}; // Initialize with an array initalizer
vector<int> v3; // Default initialization
vector<int> v4 = {8, 3, 1, 5, 12, 13, 2, 7, 10};
vector<int>::size_type vsize; // size_type is an unsigned type that stores a size
v1 = {1, 2, 3, 4};
vsize = v1.size();
cout << "Vector 1 size=" << vsize << " ";
cout << "capacity=" << v1.capacity() << endl;
cout << "Vector 2 size=" << v2.size() << " ";
cout << "capacity=" << v2.capacity() << endl;
cout << "Vector 3 size=" << v3.size() << " ";
cout << "capacity=" << v3.capacity() << endl;
cout << "Vector 4 size=" << v4.size() << " ";
cout << "capacity=" << v4.capacity() << endl;
cout << endl;
cout << "Accessing the values in a vector: " << endl;
// The following is UNSAFE
cout << " Array indexing: ";
for (unsigned int ii=0; ii<v1.capacity()+5; ii++) {
cout << v1[ii] << " ";
}
cout << endl;
cout << " at() (beyond size()): ";
try {
for (unsigned int ii=0; ii<v1.capacity()+5; ii++) {
cout << v1.at(ii) << " ";
}
}
catch (const out_of_range& excep) {
cout << "Out of range exception!";
}
cout << endl;
cout << " at() (using size()): ";
for (unsigned int ii=0; ii<v1.size(); ii++) {
cout << v1.at(ii) << " ";
}
cout << endl;
// The following is safer : use an iterator
cout << " Using an iterator:";
for (vector<int>::iterator ii=v1.begin(); ii != v1.end(); ++ii) {
cout << (*ii) << " ";
}
cout << endl;
cout << " Using a for-each: ";
for (auto const &ii : v1) {
cout << ii << " ";
}
cout << endl;
// Modifying vector entry using array indexing
v1[4] = 5;
cout << endl << "v1[4]=5" << endl;
cout << "Vector1 size = " << v1.size() << " ";
cout << "capacity = " << v1.capacity() << endl;
cout << "Contents: " << endl;
cout << " for-each: ";
for (auto const &ii : v1) {
cout << ii << " ";
}
cout << " array-indexing: ";
for (unsigned int ii=0;ii<v1.capacity(); ii++) {
cout << v1[ii] << " ";
}
cout << endl;
// Modifying vector entry using push_back()
v1.push_back(6);
cout << endl << "push_back(6)" << endl;
cout << "Vector1 size = " << v1.size() << " ";
cout << "capacity = " << v1.capacity() << endl;
cout << "Contents: " << endl;
cout << " for-each: ";
for (auto const &ii : v1) {
cout << ii << " ";
}
cout << " array-indexing: ";
for (unsigned int ii=0;ii<v1.capacity(); ii++) {
cout << v1[ii] << " ";
}
cout << endl;
cout << endl << "Calling push_back when initialized with an array " << endl;
v2.push_back(40);
v2.push_back(50);
v2.push_back(60);
printVector(v2, "v2");
cout << endl << "Calling push_back on uninitialized vector " << endl;
v3.push_back(44);
v3.push_back(55);
v3.push_back(66);
printVector(v3, "v3");
cout << endl << "Calling push_back on a vector with a defined capactity " << endl;
for (int ii=0;ii<10;ii++) {
v1.push_back(ii+10);
}
printVector(v1, "v1");
cout << endl << "Removing elements from v1" << endl;
cout << " Before : ";
printVector(v1, "v1");
v1.erase(v1.begin());
cout << " Removed first element : ";
printVector(v1, "v1");
v1.erase(v1.begin()+3);
cout << " Removed fourth element : ";
printVector(v1, "v1");
v1.erase(v1.end()-1);
cout << " Removed last element : ";
printVector(v1, "v1");
v1.erase(v1.end()-3);
cout << " Removed third-to-last element : ";
printVector(v1, "v1");
v1.erase(v1.begin(), v1.begin() + v1.size()/2);
cout << " Removed 1st half of the vector : ";
printVector(v1, "v1");
v1.pop_back();
cout << " Removed using pop_back : ";
printVector(v1, "v1");
cout << endl;
cout << "Print front & back v3:" << endl;
printVector(v3, "v3");
cout << " v3 front: " << v3.front() << endl;
cout << " v3 back: " << v3.back() << endl;
cout << endl << "Sorting a vector: " << endl;
cout << " Before: ";
printVector(v4, "v4");
sort(v4.begin(), v4.end());
cout << " After: ";
printVector(v4, "v4");
cout << endl;
return 0;
}
template<typename T>
void printVector(vector<T> vec, const string name) {
cout << "Vector " << name << " s=" << vec.size()
<< " c=" << vec.capacity();
cout << " contents: [ ";
for (auto const & ii : vec) {
cout << ii << " ";
}
cout << "]" << endl;
}