C++ Library Extensions 2022.12.09
To help learn modern C++ programming
30-map_of_variants.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
3namespace types = tpf::types;
4
8
10{
11 using key_t = const char*;
12 using name_t = std::string;
13 using age_t = int;
14 using weight_t = double;
15
17
18 // people_t is std::vector<std::map<key_t, std::variant<name_t, age_t, weight_t>>
19 using people_t = std::vector<person_t>;
20
21 auto key_name = "name";
22 auto key_age = "age";
23 auto key_weight = "weight";
24
25 people_t people; // vector of persons
26
27 people.emplace_back(person_t{ {key_name, "Thomas Kim"}, { key_age, 23 }, {key_weight, 56.0} } );
28 people.emplace_back(person_t{ {key_name, "Sophie Turner"}, { key_age, 26 }, {key_weight, 66.5} } );
29 people.emplace_back(person_t{ {key_name, "Albert Kim"}, { key_age, 21 }, {key_weight, 60.5} } );
30
31 for(auto& person: people)
32 {
33 stream << "My name is " << person[key_name] << ". " << endl;
34 stream << "I am " << person[key_age] << " years old." << endl;
35 stream << "I weigh " << person[key_weight] << " kg." << endL;
36 }
37}
38
39int main()
40{
42}
auto endL
tpf::sstream stream
void powerful_application_of_map_of_variants()
auto endl
int main()
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
hidden::unordered_map_of_variants_t< KeyType, ElementTypes... > unordered_map_of_variants_t
Definition: tpf_types.hpp:6651
constexpr auto endL
Definition: tpf_output.hpp:974
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.