queue container in C++

Level 4218 keystrokes

A queue is first in, first out: values are pushed at the back and popped from the front, so after one pop the front changes from 10 to 20.

C++ code

1queue<int> q;
// push adds each value at the back
2q.push(10);
3q.push(20);
4q.push(30);
// front is the oldest element: prints Front: 10
5cout << "Front: " << q.front() << endl;
// back is the newest element: prints Back: 30
6cout << "Back: " << q.back() << endl;
// pop removes the front element (10) and returns nothing
7q.pop();
// Prints After pop: 20
8cout << "After pop: " << q.front() << endl;
// Two elements are left: prints Size: 2
9cout << "Size: " << q.size() << endl;

Output

Front: 10
Back: 30
After pop: 20
Size: 2
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 25
  • ;× 9
  • .× 8
  • "× 8
  • (× 8
  • )× 8
  • :× 4
  • >× 1

More C++ examples at level 4

← C++ typing practice