Any STL data structure like pair that gives three items(types) instead of two?

拜拜、爱过 提交于 2019-12-05 12:58:39

问题


Question 1:

I'm using C++ 11, and I'm learning. I realize I can do this with two pairs:

pair<pair<<#class _T1#>, <#class _T2#>>, <#class _T3#>>

Is that the best way?

Question 2:

If I don't need different types, so same type for two items, is it a waste to use pair, what should I use then? For three items? (again same type)


回答1:


Use a std::tuple:

std::tuple<_T1, _T2, _T3>

Note that std::tuples support an arbitrary amount of types stored in them. Also, to access the elements, you can't do the nice pair.first/pair.second, you have to use the syntax std::get<n>(tuple), where n is the element you want to retrieve.



来源:https://stackoverflow.com/questions/15734136/any-stl-data-structure-like-pair-that-gives-three-itemstypes-instead-of-two

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