C++ Return list inside a std::pair

China☆狼群 提交于 2019-12-13 09:09:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!