C++ Deque back
C++ Deque back function is used to access the last element of the deque container.
Syntax
Example
reference back();
Parameter
It does not contain any parameter.
Return value
It returns a reference to the last element of the deque container.
Example 1
Let's see a simple example
Example
#include <iostream>
#include<deque>
using namespace std;
int main()
{
deque<int> k={1,2,3,4,5};
cout<<k.back();
return 0;
}
Output:
In this example, back function returns the last element of the deque i.e 5.
Example 2
Let's see a another simple example
Example
#include <iostream>
#include<deque>
using namespace std;
int main()
{
deque<int> n;
deque<int>::iterator itr;
n.push_back(15);
while(n.back()!=0)
{
n.push_back(n.back()-1);
}
cout<<"Content of deque:";
for(itr=n.begin();itr!=n.end();++itr)
cout<<*itr<<" ";
return 0;
}
Output:
Output
Content of deque:15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0