Welcome to the practical segment of our C++ programming journey! Today, we're applying the knowledge from past lessons to solve two practice problems using advanced C++ Standard Template Library (STL) structures: std::queue
and std::map
with custom comparison operators through class definitions.
Consider an event-driven system, like a restaurant. Orders arrive, and they must be handled in the order they were received, following the First In, First Out (FIFO) principle. This principle makes it a perfect scenario for a queue
implementation in C++.
C++1#include <iostream> 2#include <queue> 3 4class Queue { 5private: 6 std::queue<int> buffer; 7public: 8 void enqueue(int val) { 9 // Adding (enqueueing) an item to the queue 10 buffer.push(val); 11 } 12 13 int dequeue() { 14 // Removing (dequeuing) an item from the queue 15 if (!buffer.empty()) { 16 int front = buffer.front(); 17 buffer.pop(); 18 return front; 19 } 20 throw std::out_of_range("Queue is empty"); 21 } 22 23 // Checking if the queue is empty 24 bool is_empty() const { 25 return buffer.empty(); 26 } 27 28 // Checking the size (number of items) in the queue 29 size_t size() const { 30 return buffer.size(); 31 } 32}; 33 34int main() { 35 Queue q; 36 q.enqueue(1); 37 q.enqueue(2); 38 q.enqueue(3); 39 40 std::cout << "Dequeued: " << q.dequeue() << '\n'; 41 std::cout << "Queue Size: " << q.size() << '\n'; 42 43 return 0; 44}
This code demonstrates the creation and operation of a Queue
class, which leverages std::queue
to efficiently implement a queue. The Queue
class includes methods to enqueue
(add) an item, dequeue
(remove) an item, check if the queue is empty, and return the queue's size. Enqueue operations add an item to the back of the queue, while dequeue operations remove an item from the front, maintaining the First In, First Out (FIFO) principle.
For the second problem, imagine a leaderboard for a video game. Players with their scores can be represented as objects of a custom class
, then stored in a std::map
for easy and efficient access.
C++1#include <iostream> 2#include <map> 3#include <string> 4 5class Player { 6public: 7 std::string name; 8 int score; 9 10 Player(std::string n, int s) : name(n), score(s) {} 11 12 // Custom comparison operator 13 bool operator<(const Player& other) const { 14 return score == other.score ? name < other.name : score < other.score; 15 } 16}; 17 18// Defining the output for Player object 19std::ostream& operator<<(std::ostream& os, const Player& p) { 20 os << "(" << p.name << ", " << p.score << ")"; 21 return os; 22} 23 24int main() { 25 std::map<Player, int> scores; 26 27 Player player1("John", 900); 28 Player player2("Doe", 1000); 29 30 // Adding players to the map 31 scores[player1] = player1.score; 32 scores[player2] = player2.score; 33 34 // Print sorted map 35 for (const auto& [player, score] : scores) { 36 std::cout << player << '\n'; 37 } 38 39 return 0; 40}
This code snippet introduces a Player
class representing players in a video game, with custom comparison operators to allow sorting by score (primary) and name (secondary). Instances of this class are then used as keys in a std::map
, ensuring that players are stored in a manner sorted first by their scores and then by their names if scores are equal. This setup is crucial for functionalities like leaderboards, where players need to be ranked efficiently according to their performance.
We've effectively utilized C++ STL structures — std::queue
and std::map
— to solve real-world problems. This hands-on approach enriches our understanding of these fundamental techniques. More exercises to deepen your comprehension are coming up next. Happy coding!