# tuple

***

**1. Storing Heterogeneous Data**

```cpp
tuple<int, string, bool> myTuple = make_tuple(10, "John", true);
```

**2. Returning Multiple Values from a Function**

```cpp
tuple<int, string> getDetails() {
  return make_tuple(10, "John");
}
```

**3. Passing Multiple Arguments to a Function**

```cpp
void printTuple(tuple<int, string>) {
  // ...
}
```

**4. Swapping Elements**

```cpp
tuple<int, string> myTuple = make_tuple(10, "John");
get<0>(myTuple) = 20; // swaps the first element
```

**5. Element Access**

```cpp
tuple<int, string, bool> myTuple = make_tuple(10, "John", true);
int age = get<0>(myTuple);
string name = get<1>(myTuple);
```

**6. Iterate Over Elements**

```cpp
tuple<int, string, bool> myTuple = make_tuple(10, "John", true);
for (auto& element : myTuple) {
  cout << element << " ";
}
```

**7. Comparing Tuples**

```cpp
tuple<int, string, bool> myTuple1 = make_tuple(10, "John", true);
tuple<int, string, bool> myTuple2 = make_tuple(10, "John", false);
bool isEqual = (myTuple1 == myTuple2);
```

**8. Sorting Tuples (lexicographical)**

```cpp
vector<tuple<int, string>> myTuples = {make_tuple(10, "John"), make_tuple(5, "Peter")};
sort(myTuples.begin(), myTuples.end());
```

**9. Merging Tuples**

```cpp
tuple<int, string> myTuple1 = make_tuple(10, "John");
tuple<bool, char> myTuple2 = make_tuple(true, 'A');
auto mergedTuple = tuple_cat(myTuple1, myTuple2);
```

**10. Function Pointers**

```cpp
tuple<void (*)(int), void (*)(string)> myTuple = make_tuple(
  [](int x) { cout << x; },
  [](string s) { cout << s; }
);
```

**11. Lambda Expressions**

```cpp
tuple<auto, auto> myTuple = make_tuple(
  []() -> int { return 10; },
  []() -> string { return "John"; }
);
```

**12. Unpack Tuples**

```cpp
int age;
string name;
tuple<int, string> myTuple = make_tuple(10, "John");
tie(age, name) = myTuple;
```

**13. Accessing Elements by Names**

```cpp
struct Student { string name; int age; };
using StudentTuple = tuple<string, int, char>;
StudentTuple myTuple = make_tuple("John", 10, 'A');
string name = get<Student.name>(myTuple);
```

**14. Custom Data Structures with Tuples**

```cpp
struct Book { string title; string author; int pages; };
Book b = {"The Hobbit", "J.R.R. Tolkien", 295};
tuple<Book, bool, double> myData = make_tuple(b, true, 9.99);
```

**15. Array of Tuples**

```cpp
tuple<int, string>[5] myArray = {
  make_tuple(10, "John"),
  make_tuple(5, "Peter"),
  make_tuple(7, "Susan"),
  make_tuple(9, "David"),
  make_tuple(11, "Emily")
};
```

**16. Tuple as a Map Key**

```cpp
unordered_map<tuple<int, string>, string> myMap;
myMap[make_tuple(10, "John")] = "John Doe";
```

**17. Set of Tuples**

```cpp
set<tuple<int, string>> mySet;
mySet.insert(make_tuple(10, "John"));
mySet.insert(make_tuple(5, "Peter"));
```

**18. Tuple with Unordered Elements**

```cpp
using UTuple = tuple<int, string, bool, optional<double>>;
UTuple myTuple = make_tuple(10, "John", true, nullopt);
```

**19. Tuple as a Class Member**

```cpp
class Person {
public:
  Person(string name, int age) : name(name), age(age) {}
  tuple<string, int> getInfo() const { return make_tuple(name, age); }

private:
  string name;
  int age;
};
```

**20. Index a Tuple**

```cpp
tuple<int, string, bool> myTuple = make_tuple(10, "John", true);
int index = 1;
auto element = get<index>(myTuple);
```

**21. Pair**

```cpp
pair<int, string> myPair = make_pair(10, "John");
```

**22. Tuple of Arrays**

```cpp
tuple<int[], string[], bool[]> myTuple = make_tuple(
  {1, 2, 3}, {"John", "Peter", "Susan"},
  {true, false, true}
);
```

**23. Tuple of Vectors**

```cpp
tuple<vector<int>, vector<string>, vector<bool>> myTuple = make_tuple(
  {1, 2, 3}, {"John", "Peter", "Susan"},
  {true, false, true}
);
```

**24. Tuple of Sets**

```cpp
tuple<set<int>, set<string>, set<bool>> myTuple = make_tuple(
  {1, 2, 3}, {"John", "Peter", "Susan"},
  {true, false, true}
);
```

**25. Tuple of Maps**

```cpp
tuple<map<int, string>, map<string, int>, map<string, bool>> myTuple = make_tuple(
  {{1, "John"}, {2, "Peter"}, {3, "Susan"}},
  {{"John", 1}, {"Peter", 2}, {"Susan", 3}},
  {{"John", true}, {"Peter", false}, {"Susan", true}}
);
```

**26. Tuple of Lists**

```cpp
tuple<list<int>, list<string>, list<bool>> myTuple = make_tuple(
  {1, 2, 3}, {"John", "Peter", "Susan"},
  {true, false, true}
);
```

**27. Tuple of Unordered Sets**

```cpp
tuple<unordered_set<int>, unordered_set<string>, unordered_set<bool>> myTuple = make_tuple(
  {1, 2, 3}, {"John", "Peter", "Susan"},
  {true, false, true}
);
```

**28. Tuple of Unordered Maps**

```cpp
tuple<unordered_map<int, string>, unordered_map<string, int>, unordered_map<string, bool>> myTuple = make_tuple(
  {{1, "John"}, {2, "Peter"}, {3, "Susan"}},
  {{"John", 1}, {"Peter", 2}, {"Susan", 3}},
  {{"John", true}, {"Peter", false}, {"Susan", true}}
);
```

**29. Tuple of C-Style Arrays**

```cpp
tuple<int*, string*, bool*> myTuple = make_tuple(
  new int[3]{1, 2, 3},
  new string[3]{"John", "Peter", "Susan"},
  new bool[3]{true, false, true}
);
```

**30. Tuple of Pointers**

```cpp
tuple<int*, string*, bool*> myTuple = make_tuple(
  new int{10}, new string{"John"}, new bool{true}
);
```

**31. Tuple of References**

```cpp
int a = 10;
string b = "John";
bool c = true;
tuple<int&, string&, bool&> myTuple = make_tuple(a, b, c);
```

**32. Tuple of Smart Pointers**

```cpp
tuple<unique_ptr<int>, unique_ptr<string>, unique_ptr<bool>> myTuple = make_tuple(
  make_unique<int>(10), make_unique<string>("John"), make_unique<bool>(true)
);
```

**33. Tuple of Function Objects**

```cpp
tuple<function<int(int)>, function<string(string)>, function<bool(bool)>> myTuple = make_tuple(
  [](int x) { return x + 1; },
  [](string s) { return s + "!"; },
  [](bool b) { return !b; }
);
```

**34. Tuple of Iterators**

```cpp
vector<int> v = {1, 2, 3};
tuple<vector<int>::iterator, vector<int>::iterator> myTuple =
  make_tuple(v.begin(), v.end());
```

**35. Tuple of Range-based `for` Control Variable**

```cpp
tuple<int, string> myTuple;
for (auto& [x, y] : myTuple) {
  // ...
}
```

**36. Tuple as a Function Argument**

```cpp
void foo(const tuple<int, string>& myTuple) {
  // ...
}
```

**37. Tuple as a Function Return Value**

```cpp
tuple<int, string> foo() {
  return make_tuple(10, "John");
}
```

**38. Tuple as an Aggregate Initializer**

```cpp
struct Person {
  int age;
  string name;
};
Person p = {10, "John"};
```

**39. Tuple as a Struct Member**

```cpp
struct Person {
  int age;
  string name;
  tuple<int, string> getDetails() const { return make_tuple(age, name); }
};
```

**40. Tuple as a Class Template**

```cpp
template<typename T1, typename T2>
struct Pair {
  tuple<T1, T2> data;
};
```

**41. Tuple as a C-Style Union**

```c
#define UNION(T1, T2) \
  union { \
    T1 t1; \
    T2 t2; \
  }
```

**42. Tuple as a Type Family**

```cpp
template<typename T>
struct TupleSize {
  static const size_t value = tuple_size<T>::value;
};
```

**43. Tuple as a Template Metaprogramming Tool**

```cpp
template<typename T, typename... Ts>
struct PushFront {
  typedef tuple<T, Ts...> type;
};
```

**44. Tuple as a Syntax Sugar**

```cpp
#define PAIR(T1, T2) make_pair(T1, T2)
```

**45. Tuple as a Data Wrangling Tool**

```cpp
tuple<int, string> myTuple = make_tuple(10, "John");
auto [age, name] = myTuple;
```

**46. Tuple as a State Machine**

```cpp
template<typename State>
struct StateMachine {
  tuple<State, string> handleInput(char c) {
    switch (get<0>(state)) {
    // ...
    }
  }

private:
  State state;
};
```

**47. Tuple as a Custom Container**

```cpp
template<typename... Ts>
class MyContainer {
public:
  MyContainer(Ts&&... args) : tuple(std::forward<Ts>(args)...) {}

  // ...
};
```

**48. Tuple as a Data Transfer Object**

```cpp
struct PersonDTO {
  int id;
  string name;
  int age;
};
```

**49. Tuple as a Command Queue**

```cpp
tuple<string, function<void()>> myCommandQueue = make_tuple("foo", []() { /* ... */ });
```

**50. Tuple as a Cache**

```cpp
unordered_map<int, tuple<int, string>> myCache;
```
