forked from Dexter-99/Hacktober-Fest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeque.cpp
38 lines (37 loc) · 1 KB
/
Deque.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
#include<iostream>
#include<deque>
#include<bits/stdc++.h>
using namespace std;
int main()
{
//Deque insertion at front O(1) operation.
//Vector insertion at front O(1) operation.
//Same goes for deletion.
deque<string> d;
//insertion
d.push_back("hello");
d.push_back("world");
d.push_back("to");//insertion at end
cout<<d.front()<<" "<<d.back();
//returns first and last element respectively
d.push_front("start");//insertion at beginning
//pop for removal
d.pop_front();
d.pop_back();
for(int i=0;i<d.size();i++)
{
cout<<d.at(i);//throws exception when out of bound d[i]
// does not check range first
}
// d.clear();//clears the deque
d.insert(d.begin()+1,"new element");
//first parameter is iterator
for(int i=0;i<d.size();i++)
{
cout<<d.at(i);
}
d.erase(d.begin());//a element
//[) last element is not included
d.erase(d.begin(),d.begin()+2);//a range
//never try to dereference end
}