tuple with functions c 2b 2b

Solutions on MaxInterview for tuple with functions c 2b 2b by the best coders in the world

showing results for - "tuple with functions c 2b 2b"
Léonard
12 Feb 2020
1#include <tuple>
2#include <iostream>
3#include <string>
4#include <stdexcept>
5 
6std::tuple<double, char, std::string> get_student(int id)
7{
8    if (id == 0) return std::make_tuple(3.8, 'A', "Lisa Simpson");
9    if (id == 1) return std::make_tuple(2.9, 'C', "Milhouse Van Houten");
10    if (id == 2) return std::make_tuple(1.7, 'D', "Ralph Wiggum");
11    throw std::invalid_argument("id");
12}
13 
14int main()
15{
16    auto student0 = get_student(0);
17    std::cout << "ID: 0, "
18              << "GPA: " << std::get<0>(student0) << ", "
19              << "grade: " << std::get<1>(student0) << ", "
20              << "name: " << std::get<2>(student0) << '\n';
21 
22    double gpa1;
23    char grade1;
24    std::string name1;
25    std::tie(gpa1, grade1, name1) = get_student(1);
26    std::cout << "ID: 1, "
27              << "GPA: " << gpa1 << ", "
28              << "grade: " << grade1 << ", "
29              << "name: " << name1 << '\n';
30 
31    // C++17 structured binding:
32    auto [ gpa2, grade2, name2 ] = get_student(2);
33    std::cout << "ID: 2, "
34              << "GPA: " << gpa2 << ", "
35              << "grade: " << grade2 << ", "
36              << "name: " << name2 << '\n';
37}