-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_ops.cpp
More file actions
48 lines (48 loc) · 1.04 KB
/
vector_ops.cpp
File metadata and controls
48 lines (48 loc) · 1.04 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
#include<bits/stdc++.h>
using namespace std;
bool compare(int x,int y)//function used while sorting in descending order
{
return (x>y);
}
void print_loop(vector<int> v)//printing the elements based on indexing
{
for(int i=0;i<v.size();i++)
{
cout<<v[i]<<" ";
}
cout<<"\n";
}
void print_itr(vector<int> v) //printing the elements using the iterator
{
vector<int>::iterator itr;
for(itr=v.begin();itr!=v.end();itr++)
{
cout<<*itr<<" ";
}
cout<<"\n";
}
int main()
{
vector<int> v;
for(int i=0;i<5;i++)
{
int x;
cin>>x;
v.push_back(x); //pushing into vector
}
print_itr(v);
v.insert(v.begin()+1,2,8); //inserting into vector
print_itr(v);
sort(v.begin(),v.end()); //sorting a vector
print_itr(v);
sort(v.begin(),v.end(),compare);//sorting the vector into ascending order
print_itr(v);
reverse(v.begin(),v.end());//reversing a vector
print_itr(v);
v.erase(v.begin()+1,v.begin()+2);//erasing elements of the vector
print_itr(v);
v.pop_back();//popping from the back
print_itr(v);
v.clear();//clearing the whole vector
print_itr(v);
}