文章目录
- C++ 新特性学习
- try_emplace的使用
- shared_ptr 学习
C++ 新特性学习
try_emplace的使用
记录一下学习的点滴…
emplace 创建对象并且调用对象的构造函数进行构造。避免了先拷贝构造临时变量然后再移动的操作。
复制代码
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#include <iostream> #include <map> class ChatDialog { public: ChatDialog(int64_t userId, int64_t hashCode) : m_userId(userId), m_hashCode(hashCode) { std::cout << "ChatDialog constructort" << m_userId << " " << m_hashCode << std::endl; } ChatDialog(const ChatDialog& log) : m_userId(log.m_userId), m_hashCode(log.m_hashCode) { std::cout << "ChatDialog copy constructort" << m_userId << " " << m_hashCode << std::endl; } ChatDialog& operator =(const ChatDialog& log) { std::cout << "ChatDialog copy assignmentt" << m_userId << " " << m_hashCode << std::endl; } ChatDialog(ChatDialog&& log) : m_userId(log.m_userId), m_hashCode(log.m_hashCode) { std::cout << "ChatDialog move constructort" << m_userId << " " << m_hashCode << std::endl; } ChatDialog& operator =(ChatDialog&& log) { std::cout << "ChatDialog move assignmentt" << m_userId << " " << m_hashCode << std::endl; } ~ChatDialog() { std::cout << "ChatDialog destrctiont" << m_userId << " " << m_hashCode << std::endl; } [[noreturn]] void active() { } private: int64_t m_userId; int64_t m_hashCode; }; // void onDoubleClickFriendItem3(std::map<int64_t, ChatDialog>& m_ChatDialog, int64_t userId, int64_t hashCode) { auto [iter, inserted] = m_ChatDialog.try_emplace(userId, ChatDialog(userId, hashCode)); iter->second.active(); } void onDoubleClickFriendItem4(std::map<int64_t, ChatDialog>& m_ChatDialog, int64_t userId, int64_t hashCode) { auto [iter, inserted] = m_ChatDialog.try_emplace(userId, std::move(ChatDialog(userId, hashCode))); iter->second.active(); } void onDoubleClickFriendItem5(std::map<int64_t, ChatDialog>& m_ChatDialog,int64_t userId, int64_t hashCode) { auto [iter, inserted] = m_ChatDialog.try_emplace(userId, userId, hashCode); iter->second.active(); } int main() { { std::map<int64_t, ChatDialog> m_ChatDialog; std::cout << std::endl << "onDoubleClickFriendItem3" << std::endl; onDoubleClickFriendItem3(m_ChatDialog, 1, 111); onDoubleClickFriendItem3(m_ChatDialog, 2, 222); onDoubleClickFriendItem3(m_ChatDialog, 1, 333); } { std::map<int64_t, ChatDialog> m_ChatDialog; std::cout << std::endl << "onDoubleClickFriendItem4" << std::endl; onDoubleClickFriendItem4(m_ChatDialog, 3, 333); onDoubleClickFriendItem4(m_ChatDialog, 4, 444); onDoubleClickFriendItem4(m_ChatDialog, 3, 555); } { std::map<int64_t, ChatDialog> m_ChatDialog; std::cout << std::endl << "onDoubleClickFriendItem5" << std::endl; onDoubleClickFriendItem5(m_ChatDialog, 5, 555); onDoubleClickFriendItem5(m_ChatDialog, 6, 666); onDoubleClickFriendItem5(m_ChatDialog, 5, 777); } }
结果是这样的,说明emplace的时候直接原地构造,而不是移动拷贝,省很大时间。
复制代码
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
68onDoubleClickFriendItem3 ChatDialog constructor 1 111 ChatDialog move constructor 1 111 ChatDialog destrction 1 111 ChatDialog constructor 2 222 ChatDialog move constructor 2 222 ChatDialog destrction 2 222 ChatDialog constructor 1 333 ChatDialog destrction 1 333 ChatDialog destrction 2 222 ChatDialog destrction 1 111 onDoubleClickFriendItem4 ChatDialog constructor 3 333 ChatDialog move constructor 3 333 ChatDialog destrction 3 333 ChatDialog constructor 4 444 ChatDialog move constructor 4 444 ChatDialog destrction 4 444 ChatDialog constructor 3 555 ChatDialog destrction 3 555 ChatDialog destrction 4 444 ChatDialog destrction 3 333 onDoubleClickFriendItem5 ChatDialog constructor 5 555 ChatDialog constructor 6 666 ChatDialog destrction 6 666 ChatDialog destrction 5 555
shared_ptr 学习
std::unique_ptr 独占资源
std::shared_ptr 多个资源间可以共享
std::weak_ptr 对对象的弱引用,解决shared_ptr 互相引用导致的死锁问题。不增加引用计数
复制代码
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#include <iostream> #include <memory> class A: public std::enable_shared_from_this<A> { public: A() { std::cout << "A constrctor." << std::endl; } ~A() { std::cout << "A destructor." << std::endl; } std::shared_ptr<A> getSelf() { return shared_from_this(); } }; int main() { { std::shared_ptr<A> sp1(new A()); std::cout << "sp1 use count " << sp1.use_count() << std::endl; std::shared_ptr<A> sp2(sp1); std::cout << "sp1 use count " << sp1.use_count() << std::endl; std::cout << "sp2 use count " << sp2.use_count() << std::endl; sp2.reset(); std::cout << "sp1 use count " << sp1.use_count() << std::endl; std::cout << "sp2 use count " << sp2.use_count() << std::endl; { std::shared_ptr<A> sp3 = sp1; std::cout << "sp1 use count " << sp1.use_count() << std::endl; std::cout << "sp3 use count " << sp3.use_count() << std::endl; } std::cout << "sp1 use count " << sp1.use_count() << std::endl; std::shared_ptr<A> sp4 = sp1->getSelf(); std::cout << "sp1 use count " << sp1.use_count() << std::endl; std::cout << "sp4 use count " << sp4.use_count() << std::endl; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13A constrctor. sp1 use count 1 sp1 use count 2 sp2 use count 2 sp1 use count 1 sp2 use count 0 sp1 use count 2 sp3 use count 2 sp1 use count 1 sp1 use count 2 sp4 use count 2 A destructor.
最后
以上就是现代咖啡最近收集整理的关于学习C++C++ 新特性学习的全部内容,更多相关学习C++C++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复