-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.cpp
59 lines (48 loc) · 1.41 KB
/
main.cpp
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
//
// main.cpp
// MapKeyModification
//
// Created by Josef Dolezal on 27/11/2018.
// Copyright © 2018 Josef Dolezal. All rights reserved.
//
#include <iostream>
#include <map>
class X {
double d_;
public:
X() : d_(0.0) { };
X(double d) : d_(d) { std::cout << "converting: " << d << std::endl; }
X(const X& other) : d_(other.d_) { std::cout << "copy: " << d_ << std::endl; }
X(X&& other) : d_(other.d_) { std::cout << "move: " << d_ << std::endl; }
operator double() const { return d_; }
};
namespace cpp14 {
void replaceMapKey() {
std::map<int, X> m1 = {{1, 1.0}, {-1, 2.0}, {3, 3.0}};
std::map<int, X> m2 = {{4, 4.0}, {5, 5.0}};
// Replace the node key
m1.erase(m1.find(-1));
m1.emplace(2, 2.0);
// Merge maps
m1.insert(std::make_move_iterator(m2.begin()), std::make_move_iterator(m2.end()));
}
}
namespace cpp17 {
void replaceMapKey() {
std::map<int, X> m1 = {{1, 1.0}, {-1, 2.0}, {3, 3.0}};
std::map<int, X> m2 = {{4, 4.0}, {5, 5.0}};
// Update the node key
auto handle = m1.extract(-1);
handle.key() = 2;
m1.insert(std::move(handle));
// Merge maps
m1.merge(m2);
}
}
int main(int argc, const char * argv[]) {
std::cout << "C++14" << std::endl;
cpp14::replaceMapKey();
std::cout << "C++17" << std::endl;
cpp17::replaceMapKey();
return 0;
}