IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 学习C++ -> 正文阅读

[C++知识库]学习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.
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-25 21:31:04  更:2021-07-25 21:31:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/4 12:20:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码