Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Demonstration of tuple and ties on CM c++17
- //
- // g++-7 -std=c++17 tuple.cpp -o tuple
- //
- #include <iostream>
- #include <string>
- #include <tuple>
- // function returns a tuple
- // a neat way of having const as a string - number union
- auto get_mark() {
- return std::make_tuple("mark", 42);
- }
- auto get_nick() {
- return std::make_tuple("nick", 45);
- }
- main()
- {
- // this time we list each item of the tuple returned
- auto t = get_mark();
- std::cout << std::get<0>(t) << std::endl;
- std::cout << std::get<1>(t) << std::endl;
- auto u = get_nick();
- std::cout << std::get<0>(u) << std::endl;
- std::cout << std::get<1>(u) << std::endl;
- // here we use tie and return the tuple to the tie
- std::string name;
- int age;
- std::tie(name, age) = get_mark();
- std::cout << name << " is " << age << std::endl;
- std::tie(name, age) = get_nick();
- std::cout << name << " is " << age << std::endl;
- // this time we auto return the tuple to name1 and age1
- auto [name1, age1] = get_mark();
- std::cout << name1 << " is " << age1 << std::endl;
- auto [name2, age2] = get_nick();
- std::cout << name2 << " is " << age2 << std::endl;
- // this time we tie with the tuple and change its values
- auto t4 = std::make_tuple(10, 20);
- auto& [first, second] = t4;
- first += 1;
- second = 35;
- std::cout << "value is now " << std::get<0>(t4) << " second is new " << std::get<1>(t4) << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement