-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapSetSplicing.cpp
More file actions
74 lines (56 loc) · 1.65 KB
/
MapSetSplicing.cpp
File metadata and controls
74 lines (56 loc) · 1.65 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
/**
* \file main.cpp
* \brief Splicing for maps and sets
*
* \todo
*
* Moving nodes and merging containers without the overhead of expensive copies, moves, or heap
* allocations/deallocations.
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
// Moving elements from one map to another:
{
std::map<int, std::string> src {{1, "one"}, {2, "two"}, {3, "buckle my shoe"}};
std::map<int, std::string> dst {{3, "three"}};
dst.insert(src.extract(src.find(1))); // Cheap remove and insert of { 1, "one" } from `src` to `dst`.
dst.insert(src.extract(2)); // Cheap remove and insert of { 2, "two" } from `src` to `dst`.
// dst == { { 1, "one" }, { 2, "two" }, { 3, "three" } };
}
// Inserting an entire set:
{
std::set<int> src {1, 3, 5};
std::set<int> dst {2, 4, 5};
dst.merge(src);
// src == { 5 }
// dst == { 1, 2, 3, 4, 5 }
}
// Inserting elements which outlive the container:
{
auto elementFactory = []()
{
std::set<int> s;
s.emplace(10);
return s.extract(s.begin());
};
std::set<int> s2;
s2.insert(elementFactory());
}
// Changing the key of a map element:
{
std::map<int, std::string> m {{1, "one"}, {2, "two"}, {3, "three"}};
auto e = m.extract(2);
e.key() = 4;
m.insert(std::move(e));
// m == { { 1, "one" }, { 3, "three" }, { 4, "two" } }
}
// std::cout << STD_TRACE_VAR("") << std::endl;
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
#endif