-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuple_xiny.cpp
More file actions
57 lines (36 loc) · 1.54 KB
/
tuple_xiny.cpp
File metadata and controls
57 lines (36 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
///////////////////////////////////////
// Tuples (C++11 and above)
///////////////////////////////////////
#include<tuple>
// Conceptually, Tuples are similar to old data structures (C-like structs) but instead of having named data members ,
// its elements are accessed by their order in the tuple.
// We start with constructing a tuple.
//
int main()
{
// Packing values into tuple
auto first = make_tuple ( 10 , 'A' ) ;
const int maxN = 1e9;
int maxL = 15;
auto second = make_tuple ( maxN , maxL ) ;
// printing elements of 'first' tuple
cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A
// printing elements of 'second' tuple
cout << get<0>(second)<< " " << get<1>(second) << "\n"; // prints: 1000000000 15
// Unpacking tuple into variables
int first_int;
char first_char;
tie (first_int , first_char ) = first;
cout << first_int << " " << first_char << "\n"; // prints : 10 A
// We can also create tuple like this.
tuple<int , char , double> third ( 11 ,'A' , 3.14141);
// tuple_size returns number of elements in a tuple (as a constexpr)
cout << tuple_size< decltype(third)>::value << "\n"; // prints: 3
// tuple_cat concatenates the elements of all the tuples in the same order.
auto concatenated_tuple = tuple_cat( first, second ,third);
// concatenated_tuple becomes = (10 , 'A' , 1e9 , 15 , 11 , 'A' , 3.14141 )
cout << get<0>(concatenated_tuple) << "\n"; // prints: 10
cout << get<3>(concatenated_tuple) << "\n"; // prints: 15
cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A'
return 0;
}