C++ Library Extensions 2022.12.09
To help learn modern C++ programming
08-container_tuple.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
5
6namespace types = tpf::types;
7
8template<typename... Types>
10
11template<template<typename, typename...> class ContainerType, typename... ElementTypes>
12class container_of_tuples_t : public ContainerType< std::tuple<ElementTypes...> >
13{
14 public:
15 using tuple_t = std::tuple<ElementTypes...>;
16
17 using base_type = ContainerType< std::tuple<ElementTypes...> >;
18
19 using base_type::base_type;
20
22 {
23 if(ct.empty())
24 {
25 os << "{ }"; return os;
26 }
27 else
28 {
29 auto last_element = ct.cend();
30 std::advance(last_element, -1);
31
32 for(auto itr=ct.cbegin(); itr != last_element; std::advance(itr, 1))
33 os << *itr << ", ";
34
35 os << *last_element;
36
37 return os;
38 }
39 }
40};
41
42template<typename... ElementTypes>
43using vector_of_tuples_t = container_of_tuples_t<std::vector, ElementTypes...>;
44
45template<typename... ElementTypes>
46using deque_of_tuples_t = container_of_tuples_t<std::deque, ElementTypes...>;
47
48template<typename... ElementTypes>
49using list_of_tuples_t = container_of_tuples_t<std::list, ElementTypes...>;
50
51template<typename... ElementTypes>
52using set_of_tuples_t = container_of_tuples_t<std::set, ElementTypes...>;
53
55{
57 using tuple_t = typename vctr_tuples_t::tuple_t;
58
59 vctr_tuples_t vt;
60
61 vt.emplace_back(tuple_t{10, 30.0f, "Thomas Kim"});
62 vt.emplace_back(tuple_t{40, 50.0f, "Sophie Turner"});
63 vt.emplace_back(tuple_t{20, 60.0f, "Steven King"});
64
65 stream <<"vt (vector of tuples ) = " << vt << endl;
66}
67
69{
71 using tuple_t = typename set_tuples_t::tuple_t;
72
73 set_tuples_t st;
74
75 st.insert(tuple_t{10, 30.0f, "Thomas Kim"});
76 st.insert(tuple_t{40, 50.0f, "Sophie Turner"});
77 st.insert(tuple_t{20, 60.0f, "Steven King"});
78
79 stream <<"st (set of tuples ) = " << st << endl;
80
81 stream << endl;
82
83 for(auto& e: st)
84 {
85 stream << e << endl;
86 }
87}
88
90{
92 using tuple_t = typename list_tuples_t::tuple_t;
93
94 list_tuples_t lt;
95
96 lt.emplace_back(tuple_t{10, 30.0f, "Thomas Kim"});
97 lt.emplace_back(tuple_t{40, 50.0f, "Sophie Turner"});
98 lt.emplace_back(tuple_t{20, 60.0f, "Steven King"});
99
100 stream <<"lt (list of tuples ) = " << lt << endl;
101
102 stream << endl;
103
104 for(auto& e: lt)
105 {
106 stream << e << endl;
107 }
108
109 stream << endl;
110
111 for(auto& e: tpf::reverse(lt))
112 {
113 stream << e << endl;
114 }
115
116
117}
118
119int main()
120{
121 // test_container_of_tuples();
122
123 // test_set_of_tuples();
124
126}
127
128
tpf::sstream stream
void test_set_of_tuples()
auto endl
void test_list_of_tuples()
int main()
void test_container_of_tuples()
std::tuple< ElementTypes... > tuple_t
ContainerType< std::tuple< ElementTypes... > > base_type
friend tpf::sstream & operator<<(tpf::sstream &os, const container_of_tuples_t &ct)
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
decltype(auto) reverse(ContainerType &&container)
Definition: tpf_types.hpp:7959
constexpr auto endl
Definition: tpf_output.hpp:973
This type is used to manipulate type list.
Definition: tpf_types.hpp:956
Stream output operators << are implemented.