-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep37_vector_algo_1.cpp
More file actions
63 lines (53 loc) · 1.42 KB
/
practicep37_vector_algo_1.cpp
File metadata and controls
63 lines (53 loc) · 1.42 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//vector and algorithm.
int main(){
cout<<"Enter array size: ";
int size;
cin>>size;
vector<int> myvec(size);
//insert values.
for(int i=0;i<size;++i){
int a;
cin>>a;
//we can only use this after declaring the size of vector.
//or myvec.insert(myvec.begin()+i,a)
myvec.at(i)=a;//or myvec.push_back(a)
};
//display the values
for(int i=0;i<size;++i){
cout<<myvec.at(i)<<" ";
};
cout<<endl;
//sort in ascending order
sort(myvec.begin(),myvec.end());
for(int i=0;i<size;++i){
cout<<myvec.at(i)<<" ";
};
cout<<endl;
//sort in descending order
sort(myvec.begin(),myvec.end(),greater<int>());
for(int i=0;i<size;++i){
cout<<myvec.at(i)<<" ";
};
cout<<endl;
//reverse the vector
reverse(myvec.begin(),myvec.end());
for(int i=0;i<size;++i){
cout<<myvec[i]<<" "; //or myvec.at(i)
};
cout<<endl;
//a iterator which keeps the index.
//iterator index starts from 0.
vector<int>::iterator itr=myvec.begin(); //its 0 now...we need to increment for increasing the index.
//erase a element at a position.
myvec.erase(myvec.begin()+2);
for(int i=0;i<size-1;++i){
cout<<myvec.at(i)<<" "; //or myvec[i]
};
cout<<endl;
//size of vector
cout<<myvec.size();
}