C++ Access Element from the Queue

www.‮figi‬tidea.com

In C++, you can access the element at the front of a queue using the front() method. Here's an example:

#include <iostream>
#include <queue>

using namespace std;

int main() {
    queue<int> myQueue;
    
    myQueue.push(10);
    myQueue.push(20);
    myQueue.push(30);
    
    cout << "The first element in the queue is: " << myQueue.front() << endl;
    
    return 0;
}

In the example above, we first create a queue called myQueue and add three integers to it using the push() method. We then use the front() method to print the value of the first element in the queue. The output of this program would be:

The first element in the queue is: 10

Note that the front() method does not remove the element from the queue. If you want to access and remove the first element, you can use the pop() method after using front().