Cambo Freelance
HomeServicesArticlesTutorialsTeamCoursesContact
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

Send us a message

Ready to start your project? Get in touch with our team.

Contact Us

Services

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

Useful Links

  • Home
  • Services
  • Learning
  • About Us
  • Contact
  • Pricing

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Queues
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 39 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++IntermediateLesson 39 of 50CamboFreelanceJune 19, 2026

C++ Queues

Learn the C++ queue container: FIFO operations, print queue simulation, and the priority_queue for ordered processing.


Tutorials › C++ › Queues
Intermediate6 min readLesson 39 of 50

What is a Queue?

A queue is a FIFO (First-In-First-Out) container. Elements are added at the back and removed from the front — like a ticket line. Include <queue>.

#include <iostream>
#include <queue>
using namespace std;

int main() {
    queue<string> customers;

    customers.push("Alice");    // enqueue
    customers.push("Bob");
    customers.push("Carol");

    cout << "Queue size: " << customers.size() << endl;

    while (!customers.empty()) {
        cout << "Serving: " << customers.front() << endl;
        customers.pop();   // dequeue
    }
    return 0;
}

Output:

Queue size: 3
Serving: Alice
Serving: Bob
Serving: Carol

Queue Methods

MethodDescription
push(val)Add to back
pop()Remove from front
front()View front element
back()View back element
empty()True if queue is empty
size()Number of elements

Priority Queue

A priority_queue always serves the highest-priority (largest by default) element first:

#include <queue>
priority_queue<int> pq;
pq.push(3); pq.push(1); pq.push(5); pq.push(2);
while (!pq.empty()) {
    cout << pq.top() << " ";   // 5 3 2 1
    pq.pop();
}

Exercise

Simulate a print queue: add 5 print jobs to a queue and process them one by one, printing "Printing: [job]" for each.

Show Solution
#include <iostream>
#include <queue>
using namespace std;

int main() {
    queue<string> printQueue;
    printQueue.push("Report.pdf");
    printQueue.push("Invoice.docx");
    printQueue.push("Photo.jpg");
    printQueue.push("Presentation.pptx");
    printQueue.push("Letter.txt");

    while (!printQueue.empty()) {
        cout << "Printing: " << printQueue.front() << endl;
        printQueue.pop();
    }
    return 0;
}

Quiz

  1. What does FIFO stand for?

    • A) First In, Fast Out
    • B) First In, First Out
    • C) Final In, First Out
    • D) First In, Full Out
    Answer

    B) First In, First Out

Summary

  • queue is FIFO: push to back, pop from front.
  • Methods: push, pop, front, back, empty, size.
  • priority_queue processes the highest-priority element first.
← Previous: Sets Next: Stacks →
c++queuesstlintermediate
PreviousLesson 38: C++ SetsNextLesson 40: C++ Stacks
Back to All Tutorials