Site Tools


wiki:hpp-deque

Table of Contents

<deque>

<deque> (double-ended queue) is like a vector but allows efficient insertion and deletion at both ends. Iterating and random access are O(1), but not as cache-friendly as vector for linear scans.

Use it when you need to push/pop from both front and back; otherwise prefer std::vector.

Example

This example demonstrates deque's key feature: efficient insertion and removal at both the front and back of the sequence.

// compile: g++ -std=c++11 -o dequeexample dequeexample.cpp
// run: ./dequeexample
// description: push/pop from front and back
 
#include <deque>
#include <iostream>
 
int main() {
    std::deque<int> q;
 
    q.push_back(2);
    q.push_back(3);
    q.push_front(1);
 
    std::cout << "deque: ";
    for (int x : q) std::cout << x << " ";
    std::cout << "\n";
 
    q.pop_front();
    std::cout << "after pop_front: ";
    for (int x : q) std::cout << x << " ";
    std::cout << "\n";
 
    return 0;
}
wiki/hpp-deque.md · Last modified: by 127.0.0.1