I previously read of a structure or class in C++11 that allows saving two values as one, what its name?
For example, I want to save 1 and 2 together, I know it's easy to implement but if there is one already done why to implement mine :)
I previously read of a structure or class in C++11 that allows saving two values as one, what its name?
For example, I want to save 1 and 2 together, I know it's easy to implement but if there is one already done why to implement mine :)
The traditional way of storing a number of values without naming them all is to use an array:
int arr[2] = {1, 2};
C++ being what it is, you have lots of other ways to do that. You could achieve something similar with the use of std::array
, std::vector
(or other STL containers), std::tuple
, std::pair
, new int[2]
, struct {int x, y;} elms;
, or do something crazy like store 2 32bit values in a single 64bit integer. These are all suitable for different use cases depending on stuff like if the values you're trying to store have the same type, if you know how many of them you want to store at compile time, if the size is fixed, if you have a few of them or many of them, if you're trying to interface with C APIs and so on. I suggest you have a look at our C++ Book Guide and List.