Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit element from Queue without removing it

Tags:

c++

queue

I am using the queue library from C++ and I need to pick the front element from the queue and edit this element without removing it from the queue. There is any way to do this? I need to do something like this:

queue<int> myQueue;
myQueue.push(1);
myQueue.push(2);
cout << myQueue.front(); // 2
int a = myQueue.front();
a = 3;
cout << myQueue.front(); // 3

Obviously this don't work =D. Anyone knows how to do it?

like image 996
user1905910 Avatar asked Sep 03 '26 15:09

user1905910


1 Answers

front() returns a reference,

int &a = queue.front();

a = 3;
like image 112
Nim Avatar answered Sep 05 '26 05:09

Nim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!