问题
I have several elements held in a std::list
. I have to output something like std::pair<int, std::list<std::string> >
. For the int I just add the int variable in directly to the pair. Is there any way to print the list in one statement without having to use iterators? How do you do that inside a pair?
回答1:
Is there any way to print the list in one statement without having to use interators.
There sure is: If, while modifying the list, you maintain an array where you store pointers to each element contained in the list, then you can simply loop over the array to get all the objects. No need for iterators!
A more serious suggestion: Just use iterators. There's no need to use them explicitly, if you use a range based loop.
来源:https://stackoverflow.com/questions/34205355/c-return-list-inside-a-stdpair