-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathvector all operations and methods[part 2].cpp
59 lines (59 loc) · 1.43 KB
/
vector all operations and methods[part 2].cpp
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
#include <iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
int main() {
cout<<"Hello World!\n";
//creatin and initializing a vector
vector<int> d{1,2,3,45,6,9};
//inserting a element at the end
d.push_back(33);
// printing all the element
for(auto x:d)
{cout<<x<<"\t";}
cout<<endl;
//deleting the last element
d.pop_back();
for(auto x:d)
cout<<x<<"\t";
cout<<endl;
//inserting some element in the middle
d.insert(d.begin()+3,100);
for(auto x:d)
cout<<x<<"\t";
cout<<endl;
//for inserting more than 1 element
d.insert(d.begin()+2,3,0);
for(auto x:d)
cout<<x<<"\t";
cout<<endl;
//erase some of the elemet from middle
d.erase(d.begin()+4);
for(auto x:d)
cout<<x<<"\t";cout<<endl;
//for erasing more than 1 elements
d.erase(d.begin()+5,d.end());
for(auto x:d)
cout<<x<<"\t";cout<<endl;
//size of vector
cout<<d.size()<<endl;
//capacity
cout<<d.capacity()<<endl;
//resize
d.resize(9);
cout<<d.size()<<" "<<d.capacity()<<endl;
for(auto x:d)
cout<<x<<"\t";cout<<endl;
//to avoid doubling , we will use reverse function.
d.reserve(1000);
cout<<d.size()<<" "<<d.capacity()<<endl;
//getting 1st element
cout<<d.front()<<endl;
//getting the last element
cout<<d.back();
//to remove the vector
d.clear();
return 0;
}