无用知识之:std::initializer_list的秘密

ops/2025/2/3 22:39:37/

先说结论,用std::initializer_list初始化vector,内部逻辑是先生成了一个临时数组,进行了拷贝构造,然后用这个数组的起终指针初始化initializer_list。然后再用initializer_list对vector进行初始化,这个动作又触发了拷贝构造。

所以说,用initializer_list初始化,还是有优化空间的。

感觉吧,如果你真想用vector保存对象,减少一半的拷贝动作的方法:最好用vector的emplace_back把数据给move进去,或者原地初始化。

或者,就用vector保存指针

std::vector<std::string> vec1{ "ant", "bat", "cat" };

 运行到initializer_list的构造函数:

D:\DevTools\VS2017\VC\Tools\MSVC\14.16.27023\include\initializer_listtemplate<class _Elem>class initializer_list{	// list of pointers to elements
public:typedef _Elem value_type;typedef const _Elem& reference;typedef const _Elem& const_reference;typedef size_t size_type;typedef const _Elem* iterator;typedef const _Elem* const_iterator;constexpr initializer_list() noexcept: _First(nullptr), _Last(nullptr){	// empty list}constexpr initializer_list(const _Elem *_First_arg,const _Elem *_Last_arg) noexcept: _First(_First_arg), _Last(_Last_arg){	// construct with pointers}  //。。。。。。。。。。。。。。。。运行到这里。。。。。。。。。。。。。。..........};

这个std::initializer_list是怎么个事呢,它就是一个wrapper,一个viewer。注意它的构造函数,接收的是起始指针和末尾的指针。所以std::initializer_list就是保存了起终指针。所以std::initializer_list对象的拷贝,也是属于“浅拷贝”,保存的都是指针,不影响它们指向的数据。

下面的描述,说明了:

https://cplusplus.com/reference/initializer_list/initializer_list/

initializer_list objects are automatically constructed as if an array of elements of type T was allocated, with each of the elements in the list being copy-initialized to its corresponding element in the array, using any necessary non-narrowing implicit conversions.

The initializer_list object refers to the elements of this array without containing them: copying an initializer_list object produces another object referring to the same underlying elements, not to new copies of them (reference semantics).

The lifetime of this temporary array is the same as the initializer_list object.

通过这个了例子,说明了初始化initializer_list所用的起终指针,是来自于一个
“数组”,这个数组提前被拷贝构造函数初始化过了。相当于先进行了三次拷贝动作。class MyDate
{
public:MyDate()//构造函数{std::cout << "构造函数 this地址 " << this << std::endl;}~MyDate()//析构函数{std::cout << "析构函数" << std::endl;}MyDate(std::initializer_list<MyDate>& d)//initializer_list拷贝构造函数{std::cout << "initializer_list拷贝构造函数" << std::endl;}MyDate(const MyDate& d)//拷贝构造函数{std::cout << "/拷贝构造函数 scr地址 " << &d << std::endl;std::cout << "拷贝构造函数 this地址 " << this << std::endl;}MyDate& operator=(const MyDate& d)//赋值运算符重载{std::cout << "赋值运算符重载" << std::endl;return *this;}MyDate* operator&()//取地址运算符重载(&){std::cout << "取地址运算符重载(&)" << std::endl;return this;}const MyDate* operator&() const//const修饰的取地址运算符重载(const &){//std::cout << "const修饰的取地址运算符重载(const &)" << std::endl;return this;}int val;
};int main()
{
构造函数 this地址 000000000014F1C4MyDate d0; std::cout << "d0 already initialized" << std::endl;std::cout <<  std::endl;/拷贝构造函数 scr地址 000000000014F1C4  “看地址,说明用d0进行的初始化”
拷贝构造函数 this地址 000000000014F1E4MyDate d1{ d0};std::cout << "d1 already initialized" << std::endl;std::cout << std::endl;打印信息
//拷贝构造函数 scr000000000014F1C4 “看地址,说明用d0进行的初始化”
拷贝构造函数 this000000000014FDA8
/拷贝构造函数 scr000000000014F1C4 “看地址,说明用d0进行的初始化”
拷贝构造函数 this000000000014FDAC
/拷贝构造函数 scr000000000014F1C4 “看地址,说明用d0进行的初始化”
拷贝构造函数 this000000000014FDB0解释:
初始化了一个长度为3的“临时”数组,用d0进行了三次构造拷贝动作,数组中每个对象的地址分别为
000000000014FDA8
000000000014FDAC
000000000014FDB0紧接着打印:
/拷贝构造函数 scr地址 000000000014FDA8
拷贝构造函数 this地址 00000000005E3660
/拷贝构造函数 scr地址 000000000014FDAC
拷贝构造函数 this地址 00000000005E3664
/拷贝构造函数 scr地址 000000000014FDB0
拷贝构造函数 this地址 00000000005E3668解释:
这些打印信息,是把临时数组里的对象拷贝进了vector里:
vector(initializer_list<_Ty> _Ilist, const _Alloc& _Al = _Alloc()): _Mybase(_Al){	// construct from initializer_list, optional allocator_Range_construct_or_tidy(_Ilist.begin(), _Ilist.end(), random_access_iterator_tag{});}std::vector < MyDate> d2{ d0,d0,d0 };//std::vector < MyDate> d2{ d0,d0,d0 };这段代码相当于:
//std::vector<MyDate> dt;
//dt.reserve(3);
//dt.emplace_back(d0);
//dt.emplace_back(d0);
//dt.emplace_back(d0);
//std::initializer_list lst(dt.begin(), dt.end());
//std::vector < MyDate> d2(lst); //对vector用initializer_list进行初始化return 1;
}

 


http://www.ppmy.cn/ops/155410.html

相关文章

介绍使用 WGAN(Wasserstein GAN)网络对天然和爆破的地震波形图进行分类的实现步骤

以下将为你详细介绍使用 WGAN&#xff08;Wasserstein GAN&#xff09;网络对天然和爆破的地震波形图进行分类的实现步骤&#xff0c;包含代码实现和项目结题报告的大纲。 代码实现 1. 环境准备 确保你已经安装了必要的库&#xff0c;如 torch、torchvision、numpy、matplot…

89,[5]攻防世界 web Web_php_include

进入靶场 <?php // 显示当前 PHP 文件的源代码&#xff0c;方便调试或展示代码内容 show_source(__FILE__);// 从 URL 的查询字符串中获取名为 hello 的参数值&#xff0c;并将其输出到页面上 // 例如&#xff0c;当访问的 URL 为 "example.php?helloworld" 时&…

第11章:根据 ShuffleNet V2 迁移学习医学图像分类任务:甲状腺结节检测

目录 1. Shufflenet V2 2. 甲状腺结节检测 2.1 数据集 2.2 训练参数 2.3 训练结果 2.4 可视化网页推理 3. 下载 1. Shufflenet V2 shufflenet v2 论文中提出衡量轻量级网络的性能不能仅仅依靠FLOPs计算量&#xff0c;还应该多方面的考虑&#xff0c;例如MAC(memory acc…

Hive之数据定义DDL

Hive之数据定义DDL 文章目录 Hive之数据定义DDL写在前面创建数据库查询数据库显示数据库查看数据库详情切换当前数据库 修改数据库删除数据库创建表管理表(内部表)外部表管理表与外部表的互相转换 修改表重命名表增加、修改和删除表分区增加/修改/替换列信息 删除表 写在前面 …

LabVIEW如何有效地进行数据采集?

数据采集&#xff08;DAQ&#xff09;是许多工程项目中的核心环节&#xff0c;无论是测试、监控还是控制系统&#xff0c;准确、高效的数据采集都是至关重要的。LabVIEW作为一个图形化编程环境&#xff0c;提供了丰富的功能来实现数据采集&#xff0c;确保数据的实时性与可靠性…

neo4j-community-5.26.0 install in window10

在住处电脑重新配置一下neo4j, 1.先至官方下载 Neo4j Desktop Download | Free Graph Database Download Neo4j Deployment Center - Graph Database & Analytics 2.配置java jdk jdk 21 官网下载 Java Downloads | Oracle 中国 path: 4.查看java -version 版本 5.n…

复制粘贴小工具——Ditto

在日常工作中&#xff0c;复制粘贴是常见的操作&#xff0c;但Windows系统自带的剪贴板功能较为有限&#xff0c;只能保存最近一次的复制记录&#xff0c;这对于需要频繁复制粘贴的用户来说不太方便。今天&#xff0c;我们介绍一款开源、免费且功能强大的剪贴板增强工具——Dit…

Linux防火墙基础

一、Linux防火墙的状态机制 1.iptables是可以配置有状态的防火墙&#xff0c;其有状态的特点是能够指定并记住发送或者接收信息包所建立的连接状态&#xff0c;其一共有四种状态&#xff0c;分别为established invalid new related。 established:该信息包已建立连接&#x…