概述
文章目录
- C++ 新特性学习
- try_emplace的使用
- shared_ptr 学习
C++ 新特性学习
try_emplace的使用
记录一下学习的点滴…
emplace 创建对象并且调用对象的构造函数进行构造。避免了先拷贝构造临时变量然后再移动的操作。
#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的时候直接原地构造,而不是移动拷贝,省很大时间。
onDoubleClickFriendItem3
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 互相引用导致的死锁问题。不增加引用计数
#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;
}
}
A 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++ 新特性学习所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复