-1

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 :)

  • 4
    Perhaps `std::pair` or whatever types you want to store. They need not be the same. – drescherjm Dec 01 '20 at 17:36
  • 1
    And if you need more than two values, there's `std::tuple`. – Ben Voigt Dec 01 '20 at 17:36
  • 1
    If the values have the same type, there is also `std::array`. – François Andrieux Dec 01 '20 at 17:37
  • @drescherjm right, are you aware of somewhere I can find implementation for something similar? like in –  Dec 01 '20 at 17:37
  • 1
    @smith [`std::pair`](https://en.cppreference.com/w/cpp/utility/pair) is built into the language. – François Andrieux Dec 01 '20 at 17:37
  • The header should have the implementation since its a template. – drescherjm Dec 01 '20 at 17:38
  • Why are you now asking to find an implementation, when the question says you'd prefer not to unless it is necessary? It is not necessary, just use `std::pair` or a `std::tuple`. – Useless Dec 01 '20 at 17:41
  • @smith are you asking for "something similar" because you can not use the containers that are already in the standard library? There are very many types that can hold "two values". – Drew Dormann Dec 01 '20 at 17:41
  • You can even use custom type (which might allows proper naming for members). – Jarod42 Dec 01 '20 at 17:42
  • 1
    the standard library offers lots of convenience stuff, sometimes but not always just search for what you want finds you the right thing. Try "C++ pair of values" (and "C++ two values" is also just two clicks away) – 463035818_is_not_an_ai Dec 01 '20 at 17:43
  • @Bathsheba why can't `memcpy` be implemented in standard C++? `for (size_t i = 0; i < size; ++i) ((unsigned char*)dst)[i]=((unsigned char*)src)[i];` – Aykhan Hagverdili Dec 01 '20 at 18:09
  • @AyxanHaqverdili: Sorry I've left you hanging out there. I'll dig something out on this. – Bathsheba Dec 01 '20 at 18:14
  • I'm wondering if the OP is perhaps unaware that C++ includes standard libraries -- that an "implementation" for all these mentioned types can be assumed to already exist? – Drew Dormann Dec 01 '20 at 18:32
  • @DrewDormann well, the question is tagged with [stl](https://stackoverflow.com/questions/tagged/stl) – Aykhan Hagverdili Dec 01 '20 at 18:35

1 Answers1

1

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.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93