-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.cpp
More file actions
61 lines (47 loc) · 1.44 KB
/
vector.cpp
File metadata and controls
61 lines (47 loc) · 1.44 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> vect; // empty vector
vector<int> v(5); // new vector of size 5
vector<bool> v2(5, true); // new vector of size 5 with all values initialized to true
// push_back
vect.push_back(2); // insert 2 at end of vector
vect.push_back(5);
vect.push_back(8);
vect.push_back(9);
auto it = vect.begin(); // get iterator to beginning
auto it2 = vect.end(); // get iterator to end (theoretically, the element after the last element)
// size
cout << vect.size() << endl;
// is_empty
cout << vect.empty() << endl;
// reference operator
cout << vect[0] << endl;
cout << vect[1] << endl;
cout << vect[2] << endl;
// front and back
cout << vect.front() << endl;
cout << vect.back() << endl;
// erase
vect.erase(vect.begin() + 4); //delete 4th element
// sort and reverse
sort(vect.begin(), vect.end()); //sort vector
reverse(vect.begin(), vect.end()); // reverse the vector
// pop_back
vect.pop_back(); // delete last element
// looping over vectors
cout << "---------------" << endl;
for (int n : vect) {
cout << n << endl;
}
cout << "---------------" << endl;
for (auto it = vect.begin(); it != vect.end(); it++) {
cout << *it << endl;
}
cout << "---------------" << endl;
// clear
vect.clear();
}