Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 613
Arch1tect Avatar asked Mar 31 '13 20:03

Arch1tect


People also ask

Can we make pair of 3 in C++?

Another solution is to use pair class in C++ STL. We make a pair with first element as normal element and second element as another pair, therefore storing 3 elements simultaneously. // element and second element as another pair. // therefore 3 elements simultaneously.

What is a triple in C++?

C++ Basic Algorithm: Exercise-36 with SolutionIf a value appears three times in a row in an array it is called a triple.

Is a Pair a STL?

Pair is used to combine together two values that may be of different data types. Pair provides a way to store two heterogeneous objects as a single unit. It is basically used if we want to store tuples.

What is pair datatype?

Overview. A pair in C++ is described as a container that combines two elements of the same or different data types. Pair in C++ consists of two elements, first and second (must be in this order), and they are accessed using the dot (.) operator and the keyword first or second.


1 Answers

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.

like image 81
Xymostech Avatar answered Oct 25 '22 15:10

Xymostech