-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep100_vector_2.cpp
More file actions
55 lines (42 loc) · 1.32 KB
/
practicep100_vector_2.cpp
File metadata and controls
55 lines (42 loc) · 1.32 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
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> myvec1(10);
vector<int> myvec2;
for(int i=0;i<10;++i){
myvec1[i]=i;
};
for(int i=0;i<10;++i){
cout<<myvec1[i]<<" ";
}
cout<<endl;
for(int i=0;i<10;++i){
myvec2.insert(myvec2.begin()+i,i);
}
//iterator is used to access the elements of the vector.
//its like a pointer, we initialize it with the begin(),end() or any position of the vector.
//we can increment it to access the next element.
vector<int>::iterator itr=myvec2.begin();
for(int i=0;i<10;++i){
cout<<*itr<<" ";
itr++;
}
cout<<endl;
myvec1.push_back(10); //inserts at the end of the vector.
cout<<myvec1.size()<<endl; //size of the vector.
myvec2.pop_back(); //removes the last element of the vector.
cout<<myvec2.size()<<endl;
myvec1.erase(myvec1.begin()+2); //erases the element at the position 3(index=2).
cout<<myvec1.size()<<endl;
for(int i=0;i<myvec1.size();++i){
cout<<myvec1[i]<<" ";
}
cout<<endl;
myvec1.clear(); //clears the vector.
cout<<myvec1.size()<<endl;
myvec2.erase(myvec2.begin(),myvec2.begin()+5); //erases the elements from 0 to 4(n-1).
for(int i=0;i<myvec2.size();++i){
cout<<myvec2[i]<<" ";
}
}