Wednesday, October 30, 2013

C++ STL deque




Another STL template is deque. Deque is a lot like queue. The main difference between queue and deque is that you can push/pop element from the beginning of the sequence.
It is defined like this
deque<data type> name;
Suppose we have 
deque<int> q;

 Here are the main functions of deque.

q.at(int i);

Unlike queue, you can access all the elements of deque. This function returns you the element with index i.[] operator can also be used for calling an element.

q.begin()
This function returns an iterator  of the first element of the deque.

q.back();
This function returns the last element in the queue.(the one who is on rightmost position)

q.clear();
This function clears the deque. After this function is used the deque becomes completely empty.

q.empty();
This boolean finction checks if the deque is empty or no. You will get "true" if there is at least one element in the queue and "false" otherwise. 

q.end();
This function return an iterator  of the last element of the deque.

q.erase( .....)

1. 
q.erase( deque<int>::iterator it);
      This function is used to erase elements from different parts of the deque. Note that this function reciveses and iterator
2.
q.erase(deque<int>::iterator from , deque<int>::iterator to)
  This function recivese 2 iterators and erases all the elements between this 2 elements.

q.front()

This function returns the first element of the deque. (The leftmost element)

q.insert(deque<int>::iterator i, int x)

This function will insert the number x between the posotions of  i and i+1.

q.pop_back()

This function will remove the last element of the deque(the rightmost element).

q.push_back(int x)

this function  adds the element x to our deque from the end.

q.pop_front()

This function removes the first element of the deque. (The leftmost element).

q.push_front(int x)

This function adds the element x from the beginning of the deque.

q.size()

This function returns the size of the deque. (The length of the deque).

IF YOU HAVE ANY QUESTIONS LEAVE THEM IN COMMENTS, NO QUESTION WILL STAY UNANSWERED

No comments:

Post a Comment