Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

In C++, a queue is a container that stores elements in a FIFO (first-in, first-out) order. It is useful when you need to process elements in the order they were added.

Here is an example of how to use queue:

#include <iostream>
#include <queue>

using namespace std;

int main() {
  queue<int> my_queue;
  my_queue.push(1);
  my_queue.push(2);
  my_queue.push(3);
  
  while (!my_queue.empty()) {
    int element = my_queue.front();
    my_queue.pop();
    cout << element << " ";
  }
  
  return 0;
}

In this example, we create a queue<int> object called my_queue and add three elements to it using the push method. We then use a while loop to process each element in the queue by removing the front element using the front method and then removing it using the pop method. Finally, we print each element to the console.

queue provides several methods for adding, removing, and retrieving elements, such as push, pop, and front. It also provides methods for checking whether the queue is empty, such as empty.

queue is often used to implement data structures like queues and buffers, which are used to store and process data in a specific order. It is useful when you need to process elements in the order they were added, such as in a message queue or a task queue.

  • No labels