Why Tuple?
September 21, 2021
C++ 11 introduces a template Tuple, similar to the one in Python, allows you to store data of different types in a single entity. But you may ask, in C/C++,we already have some mechaism called struct allows us to do the same thing, then why do we need tuple?
Case 1: One-Time Structure to Transfer Data
1 | tuple<string,int> getNameAge() |
If we want to transfer a group of data for only one time, we may not want to define a struct, and using tuple get us away from these definitions.
Case 2: Fast Comparison Using Tuple Operators
1 | tuple<int,int,int> time1,time2; |
Case 3: Multi-Index Map/Unordered Map
1 | //define |
Case 4: Fast Swap
1 | int a,b,c; |
Conclusion:
Tuple is not recommended to be overused. If needed, use struct instead.
Load Comments