学习C++

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  constructor\t" << m_userId << " " << m_hashCode << std::endl;
	}
	ChatDialog(const ChatDialog& log) : m_userId(log.m_userId), m_hashCode(log.m_hashCode)
	{
		std::cout << "ChatDialog  copy constructor\t" << m_userId << " " << m_hashCode << std::endl;
	}
	ChatDialog& operator =(const ChatDialog& log)
	{
		std::cout << "ChatDialog  copy assignment\t" << m_userId << " " << m_hashCode << std::endl;
	}
	ChatDialog(ChatDialog&& log) : m_userId(log.m_userId), m_hashCode(log.m_hashCode)
	{
		std::cout << "ChatDialog  move constructor\t" << m_userId << " " << m_hashCode << std::endl;
	}
	ChatDialog& operator =(ChatDialog&& log)
	{
		std::cout << "ChatDialog  move assignment\t" << m_userId << " " << m_hashCode << std::endl;
	}

	~ChatDialog()
	{
		std::cout << "ChatDialog  destrction\t" << 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值