Principle: FIFO (First In First Out)

Header: <queue>, <deque>

Methods: push(), pop(), front(), empty(), push_front(), push_back(), pop_front(), pop_back(), end(), rend()

Associated: std::rotate() - deque

Example:

In this exercise, you will develop a simple coffee shop order management system using a deque in C++. You will perform the following tasks:

Initialize a deque to manage coffee shop orders with initial drinks: 'Latte', 'Espresso', and 'Cappuccino'.
Add a new order 'Mocha' to the end of the deque.
Simulate a customer's change of mind by moving 'Latte' from the front to the back of the deque.
Serve (remove and print) the first order in the deque, which should be 'Espresso'.
Your goal is to demonstrate effective use of deque operations to simulate line management in a coffee shop.
#include "solution.hpp"

int main() {
    // TODO: Create a deque for coffee shop orders with initial orders 'Latte', 'Espresso', 'Cappuccino'
    std::deque<std::string> orders = {"Latte", "Espresso", "Cappuccino"};

    // TODO: A new customer orders a 'Mocha', add it to the end of the deque
    orders.push_back("Mocha");

    // TODO: A customer wants their 'Latte' order moved to the end of the deque. Implement it.
    std::rotate(orders.rbegin(), orders.rbegin()+1, orders.rend());

    // TODO: Process (remove and print) the first order in the deque. Which drink is it?
    std::string order = orders.front();
    orders.pop_front();
    std::cout << order << std::endl;
    
    return 0;
}

'테크 > CodeSignal-C++' 카테고리의 다른 글

6. Browsing system for Deque  (0) 2024.11.30
5. Map using custom object key  (0) 2024.11.29
4. Multimap to Map  (0) 2024.11.28
3. Map & Multimap  (0) 2024.11.26
1. Stack  (0) 2024.11.23

+ Recent posts