Skip to content

Commit 051ff84

Browse files
authored
Update 010_map_merge.cpp
1 parent 7f4f2b4 commit 051ff84

File tree

1 file changed

+12
-36
lines changed

1 file changed

+12
-36
lines changed

cpp_17/010_map_merge.cpp

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,17 @@
1+
#include <map>
12
#include <iostream>
2-
#include <iterator>
3-
#include <algorithm>
4-
#include <vector>
5-
#include <random>
6-
#include <functional>
3+
#include <string>
74

85
int main()
96
{
10-
// fill the vectors with random numbers
11-
std::random_device rd;
12-
std::mt19937 mt(rd());
13-
std::uniform_int_distribution<> dis(0, 9);
14-
15-
std::vector<int> v1(10), v2(10);
16-
std::generate(v1.begin(), v1.end(), std::bind(dis, std::ref(mt)));
17-
std::generate(v2.begin(), v2.end(), std::bind(dis, std::ref(mt)));
18-
19-
// sort
20-
std::sort(v1.begin(), v1.end());
21-
std::sort(v2.begin(), v2.end());
22-
23-
// output v1
24-
std::cout << "v1 : ";
25-
std::copy(v1.begin(), v1.end(), std::ostream_iterator<int>(std::cout, " "));
26-
std::cout << '\n';
27-
28-
// output v2
29-
std::cout << "v2 : ";
30-
std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, " "));
31-
std::cout << '\n';
32-
33-
// merge
34-
std::vector<int> dst;
35-
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));
36-
37-
// output
38-
std::cout << "dst: ";
39-
std::copy(dst.begin(), dst.end(), std::ostream_iterator<int>(std::cout, " "));
40-
std::cout << '\n';
7+
std::map<int, std::string> ma {{1, "apple"}, {5, "pear"}, {10, "banana"}};
8+
std::map<int, std::string> mb {{2, "zorro"}, {4, "batman"}, {5, "X"}, {8, "alpaca"}};
9+
std::map<int, std::string> u;
10+
u.merge(ma);
11+
std::cout << "ma.size(): " << ma.size() << '\n';
12+
u.merge(mb);
13+
std::cout << "mb.size(): " << mb.size() << '\n';
14+
std::cout << "mb.at(5): " << mb.at(5) << '\n';
15+
for(auto const &kv: u)
16+
std::cout << kv.first << ", " << kv.second << '\n';
4117
}

0 commit comments

Comments
 (0)