Skip to content

Commit cb65741

Browse files
authored
Update 010_map_merge.cpp
1 parent 0898d8e commit cb65741

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

cpp_17/010_map_merge.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
1-
1
1+
#include <iostream>
2+
#include <iterator>
3+
#include <algorithm>
4+
#include <vector>
5+
#include <random>
6+
#include <functional>
7+
8+
int main()
9+
{
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';
41+
}

0 commit comments

Comments
 (0)