rammstein
rammstein

Reputation: 57

How to insert from QUEUE to 2D LIST

I have a pretty basic question. How to push from QUEUE to LIST. I am pretty much copying from queue to list. Here is my example:

list<list<BoardingTicket>> boardPassenger(queue<BoardingTicket>& waitingLine){
     list< list<BoardingTicket>> combinedList;
     while(!waitingLine.empty()){
          combinedList.push_back(waitingLine.front()); 
          waitingLine.pop();
     }

     return combinedList;
}

Is it any easier way or do I need to do it with iterators.

Thanks in advance!

EDIT: LINE 4 (combinedList.push_back(waitingLine.front());) DOESN'T WORK!

Upvotes: 0

Views: 68

Answers (1)

asmmo
asmmo

Reputation: 7100

If u want to use iterators on std::queue<T>, it isn't possible. There are no iterators for them.

If u want to use the iterator of the list but I think push_back() is easier and more clear.

Use the following

list<list<BoardingTicket>> boardPassenger(queue<BoardingTicket>& waitingLine){
     list<BoardingTicket> combinedList;
     while(!waitingLine.empty()){
          combinedList.insert(combinedList.begin(), waitingLine.front());
          waitingLine.pop();
     }

     return list<list<BoardingTicket>>{std::move(combinedList)};//#include<utility>
}

Upvotes: 1

Related Questions