-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTransform.cpp
More file actions
118 lines (97 loc) · 3.62 KB
/
Transform.cpp
File metadata and controls
118 lines (97 loc) · 3.62 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// =====================================================================================
// Transform.cpp // std::transform
// =====================================================================================
module modern_cpp:transform;
namespace AlgorithmTransform {
static void test_01()
{
// very simple phone book
std::cout << "List of Entries: " << std::endl;
std::unordered_map<std::string, std::size_t> phonebook
{
{ "Hans Meier" , 12345678 },
{ "Franz Schneider", 81726354 },
{ "Hubert Mueller", 87654321 }
};
for (const auto& [name, number] : phonebook) {
std::cout << name << ": " << number << std::endl;
}
std::vector<std::string> names(phonebook.size()); // set size of vector (!)
// std::transform on a single range - retrieve names from phonebook
std::transform(
phonebook.begin(),
phonebook.end(),
names.begin(), // beginning of the destination range
[](const std::pair<const std::string, std::size_t>& entry) {
return std::get<0>(entry);
}
);
std::cout << "List of Persons: " << std::endl;
for (const auto& name : names) {
std::cout << name << std::endl;
}
}
static void test_02()
{
// very simple phone book
std::cout << "List of Entries: " << std::endl;
std::unordered_map<std::string, std::size_t> phonebook
{
{ "Hans Meier" , 12345678 },
{ "Franz Schneider", 81726354 },
{ "Hubert Mueller", 87654321 }
};
for (const auto& [name, number] : phonebook) {
std::cout << name << ": " << number << std::endl;
}
std::vector<std::string> names{}; // empty vector (!)
// std::transform on a single range - retrieve names from phonebook
std::transform(
phonebook.begin(),
phonebook.end(),
std::back_inserter(names), // back_inserter needed (!)
[](const std::pair<const std::string, std::size_t>& entry) {
return std::get<0>(entry);
}
);
std::cout << "List of Persons: " << std::endl;
for (const auto& name : names) {
std::cout << name << std::endl;
}
}
static void test_03()
{
std::vector<std::string> persons
{
std::string{ "Hans Meier" },
std::string{ "Hubert Mueller" },
std::string{ "Franz Schneider" }
};
std::vector<std::size_t> numbers{ 12345678, 7654321, 81726354 };
std::unordered_map<std::string, std::size_t> contacts{};
// std::transform on two ranges - create contacts from separate informations
std::transform(
persons.cbegin(),
persons.cend(),
numbers.begin(),
std::inserter(contacts, contacts.end()),
[](const std::string& person, std::size_t number) {
return std::pair<const std::string, std::size_t>{ person, number };
}
);
std::cout << "List of Contacts: " << std::endl;
for (const auto& [name, number] : contacts) {
std::cout << name << ": " << number << std::endl;
}
}
}
void main_transform()
{
using namespace AlgorithmTransform;
test_01();
test_02();
test_03();
}
// =====================================================================================
// End-of-File
// =====================================================================================