c++列表初始化

发布于:2025-07-20 ⋅ 阅读:(15) ⋅ 点赞:(0)

测试代码 :

#define _CRT_SECURE_NO_WARNINGS
#include<vector>
#include<iostream>
using namespace std;

struct my_struct 
{
    int a;
    char b;
    string c;
};

class my_class 
{
public:
    my_class(int x)
    {
	_a = _b = x;
	cout << "call  my_class(int x)" << endl;

    }
    my_class(int x,char y) 
    {
	_a = x;
	_b = y;
	cout << "call  my_class(int x,char y)" << endl;
    }
    void push_back(int x,int y) 
    {
	cout << "call  push_back(int x,int y)" << endl;
    }
    void insert(int x) 
    {
	cout << "call insert(int x)" << endl;
    }
    int _a;
    char _b;
};

int main() 
{
    /*
     * 在C++98中,标准允许使用花括号{}对数组或者
     *结构体元素进行统一的列表初始值设定
     */
    int arr1[3] = {1,2,3};
    string arr2[] = {"abc","def","ghi"}; //使用初始化列表后可以不用指定数组元素个数,初始化列表有几个元素,数组默认就有几个元素空间
    my_struct a_struct = {1,'a',"abcd"};//其实就相当于结构体默认有一个三个参数的构造函数,如果在结构体写了默认构造函数,这个构造函数就不会生成,这个语句就会执行失败。用class声明结构体则不会自定生成这样的构造函数。

    for (auto& e : arr1) { cout << e << " ";}
    cout << endl;
    for (auto& e : arr2) { cout << e << " "; }
    cout << endl;
    cout << a_struct.a << " " << a_struct.b << " " << a_struct.c;
    cout << endl;

    cout << "-----------------------------------------------" << endl;
//---------------------------------------------------------------------------------------------------------------------------------------------    
   
    /*C++11扩大了用大括号括起的列表(初始化列表)的使用范围
     ,使其可用于所有的内置类型和用户自定义的类型 使用初始
     化列表时,可添加等号(=),也可不添加。
    */
    int x1 = 1;
    int x2 = { 1 };
    int x3{ 1 };
    my_class a_class = { 2,'a' };//调用了构造函数
    my_class b_class{ 3,'b' };//调用了构造函数
    int arr3[2]{1,2};
    cout << x1 << " " << x2 << " " << x3;
    cout << endl;
    cout << a_class._a << " " << a_class._b <<" " << b_class._a << " " << b_class._b;
    cout << endl;
    for (auto& e : arr3) { cout << e << " "; };
    cout << endl;

    cout << "-----------------------------------------------" << endl;
//---------------------------------------------------------------------------------------------------------------------------------------------    

    /*
     * 
     */
    std::vector<my_class> my_vector1 = { 1,{1,2},3};//在这里用初始化列表初始化my_vector1,这依赖于vector内部实现以initial_list<T>为参数的构造函数,initial_list<T>可以内部成
                                                    //员是两个指针,一个指向初始化列表(也就是{})的头,一个指向初始化列表的尾,由此,在使用初始化列表vector时可以任意个参数,
                                                    //1和3调用的是my_class(int x)实现初始化,{1,2}调用的是my_class(int x,int y)实现初始化。
    my_vector1.push_back(1);//这里其实是单参数隐式类型转换,1通过my_class(int x)转换成了my_class类型的临时对象,然后在把临时对象作为参数床底给push_back函数。
    my_vector1.push_back({1,2});//这里其实是多参数隐式类型转换,{1,2}通过my_class(int x,int y)转换成了my_class类型的临时对象,然后在把临时对象作为参数床底给push_back函数。--调用两个参数的构造函数就用{}

    for (auto& e : my_vector1) { cout << e._a << ":"<<e._b<<" "; };
    cout << endl;



    //实际上,可以把数组看做一个内部实现了initial_list<T>为参数的构造函数的类。因此可以用类似这样的方式初始化:int arr[] = {1,2,3}
    // 可以把内置类型看做有构造函数的类因此,内置类型也可以用类似这样的方式初始化:int x(5);int y{1},int(3)--匿名int对象。
}

运行结果如下:


网站公告

今日签到

点亮在社区的每一天
去签到