Codectionary / Developer documentation / C++

C++ std::deque at both ends

deque is a sequence container with efficient insertion and removal at both ends and indexed access. Its storage is not required to be contiguous like vector storage. This example uses C++11 or later.

Syntax

std::deque<T> items; items.push_front(value); items.push_back(value);

Examples

C++ std::deque at both ends

Save as main.cpp and compile with a C++20 compiler, for example g++ -std=c++20 main.cpp -o example. The article states the minimum language version.

#include <deque>
#include <string>
#include <iostream>
int main() {
    std::deque<std::string> jobs{"normal"};
    jobs.push_front("urgent");
    jobs.push_back("later");
    std::cout << jobs.front() << '\n';
    jobs.pop_front();
    std::cout << jobs.front() << '\n';
}
  1. Output: urgent, then normal. Popping the front reveals the next queued item.

Best practices

  • Check empty before accessing the front. Do not create a span from deque elements or assume pushes preserve iterators.

Runtime & versions

Compiled native applications. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

ISO C++ resources

Specifications & further reading