C++11中的{}與std::initializer_list深度解析
更新時間:2025年11月18日 10:23:24 作者:學(xué)困昇
C++11引入了統(tǒng)一的初始化方式,使用{}進(jìn)行初始化,支持內(nèi)置類型和自定義類型,C++11還引入了std::initializer_list類,支持對容器的初始化,方便地用多參數(shù)構(gòu)造對象,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
1.C++11中的{}
1.1.C++98中傳統(tǒng)的{}
用于對一般數(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開始,想要統(tǒng)一初始化方式,實現(xiàn)一切對象都可用{}進(jìn)行初始化,用{}初始化也叫列表初始化
- 內(nèi)置類型和自定義類型均支持用{}初始化,自定義類型本質(zhì)是隱式類型轉(zhuǎn)換,會產(chǎn)生臨時變量(優(yōu)化以后變成直接構(gòu)造)
- 用{}初始化,=可以省略
- C++11中列表初始化統(tǒng)一了初始化方,在用多參數(shù)構(gòu)造對象時,用{}初始化(相比有名對象和匿名對象傳參)會方便很多
#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 }臨時變量
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 });
//相比有名對象和匿名對象傳參,{}更加方便
students.push_back({ "WangPeng", 20251117, 19 });
return 0;
}2.std::initializer_list
- 通過{}初始化已經(jīng)很方便了,但是對象容器的初始化還是不太方便,假設(shè)想對一個vector對象進(jìn)行初始化,要用N個值構(gòu)造初始化,那么需要構(gòu)造多次才能實現(xiàn)(因為容器中元素數(shù)量不確定,所以每個元素都要執(zhí)行對應(yīng)的構(gòu)造函數(shù))
vector<int> = {1, 2, 3, 4};- C++11庫中,新增了一個std::initializer_list類,它的本質(zhì)是底層開一個數(shù)組,將數(shù)據(jù)拷貝到數(shù)組中,其中包含兩個指針分別指向開始(begin)和結(jié)束(end)
auto il = {98, 99, 100};- initializer_list支持迭代器遍歷
- STL中的容器支持std::initializer_list的構(gòu)造函數(shù)(即支持任意多個值構(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中只存了兩個首尾指針(在64位環(huán)境下大小均為8個字節(jié))
cout << sizeof(mylist) << endl;//輸出16
//首尾指針地址與變量i地址接近 說明該數(shù)組存儲在棧上
cout << mylist.begin() << endl;
cout << mylist.end() << endl;
cout << &i << endl;
//直接構(gòu)造
vector<int> v1({ 1,2,3,4,5 });
//構(gòu)造臨時對象->臨時對象拷貝給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","一個"},{"stack","棧"},{"queue","隊列"} };
//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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中經(jīng)socket接收數(shù)據(jù)的相關(guān)函數(shù)詳解
這篇文章主要介紹了C語言中經(jīng)socket接收數(shù)據(jù)的相關(guān)函數(shù)詳解,分別為recv()函數(shù)和recvfrom()函數(shù)以及recvmsg()函數(shù)的使用,需要的朋友可以參考下2015-09-09
QT Creator+OpenCV實現(xiàn)圖像灰度化的示例代碼
這篇文章主要為大家詳細(xì)介紹了QT如何利用Creator和OpenCV實現(xiàn)圖像灰度化效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-12-12
VSCode下.json文件的編寫之(1) linux/g++ (2).json中參數(shù)與預(yù)定義變量的意義解釋
這篇文章主要介紹了VSCode下.json文件的編寫之(1) linux/g++ (2).json中參數(shù)與預(yù)定義變量的意義解釋,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

