-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorDemo
116 lines (61 loc) · 1.36 KB
/
vectorDemo
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
vector:
it is mixture of array and linkedlist.
declarations:
vector<int>v;
vector<int>v{1,2,3}
vector<int>v={1,2,3}
for use :
simple storage
Quick lookup by index
Serialization
efficient traversal
don't use for
insertion deletion for middle element
non integer indexing
Time complextiy
insertion head O(n)
insertion index O(n)
insert tail O(1)
Remove head O(n)
Remove index O(n)
Remove tail O(1)
find object o(n)
find index O(1)
operation syntax
insert at head
v.insert(v.begin,value)
insert at any index
v.insert(v.begin()+index,value) //index with 0
insert at tail
v.push_back(value)
Acess the value :
int head=v.front();
int value =v.at(index) or v[index];
int tail=v.back();
int size=v.size();
reverse the vector :
reverse(v.begin(),v.end());
//for traverse
for (auto x:v)
cout<<x;
or
vector<int>iterator it;
for(it=v.begin();it!=v.end();it++)
cout<<*it;
//for binary search
bool y=binary_search(v.begin(),v.end(),value);
//lowerbound and upper bound
auto lower=lower_bound(v.begin(),v.end(),value);
auto upper=upper_bound(v.begin(),v.end(),value);
//sort
v.sort(v.begin(),v.end());
you can use comprator
for sort a vector
v.sort(v.begin(),v.end(),comparator);
//erase and clear
v.erase(v.begin());
v.erase(v.begin()+index); //indexing from 0
v.pop_back();
v.clear();
size=0;
alot much more function but these are important .