Codectionary / Developer documentation / C++

C++ std::queue FIFO jobs

queue restricts a sequence to first-in, first-out operations, making the intended processing order explicit. front reads the next value and pop removes it without returning it. This example uses C++11 or later.

Syntax

jobs.push(value); auto next = jobs.front(); jobs.pop();

Examples

C++ std::queue FIFO jobs

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 <queue>
#include <string>
#include <iostream>
int main() {
    std::queue<std::string> jobs;
    jobs.push("download");
    jobs.push("index");
    while (!jobs.empty()) {
        std::cout << jobs.front() << '\n';
        jobs.pop();
    }
}
  1. Output: download, then index. Every front access is protected by an empty check.

Best practices

  • Copy or move the current value before popping if later code needs it. queue is not automatically safe for concurrent producers.

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