Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to access back data from queue in C++ through linked list implementation.
I used STL::back function for this.
C++
#include <iostream>  
#include <queue>  
int main()  
{  
    std::queue<int> myQ;  
    myQ.push(10);  
    myQ.push(20); 
    myQ.push(30);  
    myQ.push(40);
    std::cout <<"newqueue.back():  "<< myQ.back ();  
    return 0;  
}


What I have tried:

I have tried STL back function.

#include <iostream>
#include <queue>
int main()
{
std::queue<int> myQ;
myQ.push(10);
myQ.push(20);
myQ.push(30);
myQ.push(40);
std::cout <<"newqueue.back(): "<< myQ.back ();
return 0;
}
Posted
Updated 8-Jun-21 3:23am
v2
Comments
Richard MacCutchan 8-Jun-21 9:05am    
What is the question?
CPallini 8-Jun-21 9:23am    
Your code shows that C++ Standard Library works, after all. Now what is the problem?
Do you have to implement yourself the linked list?

1 solution

The std::queue<T,Container>::back[^] function accesses the back of the queue: the last element pushed onto it.

If you want the oldest element, you would need to use std::queue<T,Container>::front[^] instead (or pop it off).

If that isn't what is giving you difficulties, you will have to be a lot clearer about your problem!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900