-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8th_vector.cpp
More file actions
68 lines (49 loc) · 1.49 KB
/
Copy path8th_vector.cpp
File metadata and controls
68 lines (49 loc) · 1.49 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
#include <iostream>
#include <vector> // <bits/c++.h> in coding context
using namespace std;
/*vector can resize and dynamicaly allocation
static allocation is in compile time stores in stack
dynamic allocation is in run time and stores in heap
two types
1. size -- no. of elements || internal array double every time
2. capacity*/
int main(){
// vector<int> vec = {1,2,3};
// cout << vec[0];
// vector<int> vec (5,0);
vector<char> vec = {'a','b','c','d','e'};
cout << "size = " << vec.size() << endl;
for(char val : vec){ // for each loop
cout << val << endl;
}
vector<int> vec2;
cout << "size = " << vec2.size() << endl;
vec2.push_back(25);
vec2.push_back(35);
vec2.push_back(45);
vec2.push_back(55);
vec2.push_back(65);
cout << "after push back size = " << vec2.size() << endl;
vec2.pop_back();
for(int val : vec2) {
cout << val << endl;
}
cout << endl;
cout << vec2.front() << endl;
cout << vec2.back() << endl;
cout << vec2.at(1) << endl;
cout << endl;
// static vs dynamic allocation
cout << "size = " << vec2.size() << endl;
cout << "capacity = " << vec2.capacity()<< endl;
vector <int> v1 = { 1,3,4,5,6};
v1.insert(v1.begin(), 5);
v1.erase(v1.begin());
for(int i =0; i<v1.size() ; i++){
cout << v1[i] << endl;
}
for (auto itr = v1.begin(); itr != v1.end(); ++itr) {
cout << *itr << " "; // dereference
}
return 0;
}