最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++11中的{}與std::initializer_list深度解析

 更新時(shí)間:2025年11月18日 10:23:24   作者:學(xué)困昇  
C++11引入了統(tǒng)一的初始化方式,使用{}進(jìn)行初始化,支持內(nèi)置類型和自定義類型,C++11還引入了std::initializer_list類,支持對(duì)容器的初始化,方便地用多參數(shù)構(gòu)造對(duì)象,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

1.C++11中的{}

1.1.C++98中傳統(tǒng)的{}

用于對(duì)一般數(shù)組結(jié)構(gòu)體的初始化

struct A {
	int _x;
	int _y;
};
int main() 
{
	int arr1[] = { 1,2,3,4,5,6 };
	int arr2[] = { 0 };
	A a = { 3,4 };
	return 0;
}

1.2.C++11中的{}

  • 從C++11開(kāi)始,想要統(tǒng)一初始化方式,實(shí)現(xiàn)一切對(duì)象都可用{}進(jìn)行初始化,用{}初始化也叫列表初始化
  • 內(nèi)置類型和自定義類型均支持用{}初始化,自定義類型本質(zhì)是隱式類型轉(zhuǎn)換,會(huì)產(chǎn)生臨時(shí)變量(優(yōu)化以后變成直接構(gòu)造)
  • 用{}初始化,=可以省略
  • C++11中列表初始化統(tǒng)一了初始化方,在用多參數(shù)構(gòu)造對(duì)象時(shí),用{}初始化(相比有名對(duì)象和匿名對(duì)象傳參)會(huì)方便很多
#include <iostream>
#include <vector>
using namespace std;
struct A {
	int _x;
	int _y;
};
class Student {
public:
	Student(const string& name, const int id = 0, const int age = 0)
		:_name(name)
		,_id(id)
		,_age(age){}
	Student(const Student& s) 
		:_name(s._name)
		,_id(s._id)
		,_age(s._age){ }
private:
	string _name;
	int _id;
	int _age;
};
int main() 
{
	// C++98中的{}
	int arr1[] = { 1,2,3,4,5,6 };
	int arr2[] = { 0 };
	A a = { 3,4 };
	//C++11中的{}
	//內(nèi)置類型初始化
	int a = { 1 };
	//自定義類型初始化
	Student stu1 = { "WangPeng", 20251117, 19 };
	//這里stu2引用的是{ "YiYi", 20258888, 18 }臨時(shí)變量
	const Student& stu2 = { "YiYi", 20258888, 18 };
	//只有{}初始化,才可以省略=
	double num{ 3.14159 };
	Student s{ "ZhangWei", 20236666, 22 };
	vector<Student> students;
	students.push_back(stu1);
	students.push_back(Student{ "WangPeng", 20251117, 19 });
	//相比有名對(duì)象和匿名對(duì)象傳參,{}更加方便
	students.push_back({ "WangPeng", 20251117, 19 });
	return 0;
}

2.std::initializer_list

  • 通過(guò){}初始化已經(jīng)很方便了,但是對(duì)象容器的初始化還是不太方便,假設(shè)想對(duì)一個(gè)vector對(duì)象進(jìn)行初始化,要用N個(gè)值構(gòu)造初始化,那么需要構(gòu)造多次才能實(shí)現(xiàn)(因?yàn)槿萜髦性財(cái)?shù)量不確定,所以每個(gè)元素都要執(zhí)行對(duì)應(yīng)的構(gòu)造函數(shù))
vector<int> = {1, 2, 3, 4};
  • C++11庫(kù)中,新增了一個(gè)std::initializer_list類,它的本質(zhì)是底層開(kāi)一個(gè)數(shù)組,將數(shù)據(jù)拷貝到數(shù)組中,其中包含兩個(gè)指針?lè)謩e指向開(kāi)始(begin)和結(jié)束(end)
auto il = {98, 99, 100};
  • initializer_list支持迭代器遍歷
  • STL中的容器支持std::initializer_list的構(gòu)造函數(shù)(即支持任意多個(gè)值構(gòu)成的{x1, x2, x3, …}進(jìn)行初始化)
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main()
{
	std::initializer_list<int> mylist;
	mylist = { 23,24,25,26,27 };
	int i = 0;
	//my_list中只存了兩個(gè)首尾指針(在64位環(huán)境下大小均為8個(gè)字節(jié))
	cout << sizeof(mylist) << endl;//輸出16 
	//首尾指針地址與變量i地址接近 說(shuō)明該數(shù)組存儲(chǔ)在棧上
	cout << mylist.begin() << endl;
	cout << mylist.end() << endl;
	cout << &i << endl;
	//直接構(gòu)造
	vector<int> v1({ 1,2,3,4,5 });
	//構(gòu)造臨時(shí)對(duì)象->臨時(shí)對(duì)象拷貝給v2->優(yōu)化為直接構(gòu)造
	vector<int> v2 = { 6,7,8,9,10 };
	const vector<int>& v3 = { 11,22,33,44,55 };
	//pair類型的{}初始化 + map的initializer_list構(gòu)造
	map<string, string> dict = { {"a","一個(gè)"},{"stack","棧"},{"queue","隊(duì)列"} };
	//initializer_list賦值可以這樣寫
	v1 = { 111,222,333,444,555 };
	return 0;
}

到此這篇關(guān)于C++11中的{}與std::initializer_list的文章就介紹到這了,更多相關(guān)c++ std::initializer_list內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

布尔津县| 图片| 绥阳县| 东阿县| 东乡族自治县| 万宁市| 醴陵市| 凤山市| 萝北县| 德安县| 广汉市| 沂水县| 石渠县| 城固县| 鄂托克旗| 石嘴山市| 林周县| 义马市| 德格县| 满洲里市| 青岛市| 林甸县| 深州市| 鹤庆县| 克山县| 句容市| 新巴尔虎右旗| 南康市| 息烽县| 维西| 平阴县| 岑溪市| 云和县| 金秀| 资溪县| 洞口县| 福州市| 台湾省| 滕州市| 东港市| 杨浦区|