简体   繁体   中英

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!

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>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM