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();
}
}- 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