重学C++ 初始化列表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/artisans/article/details/81260295
// 

#include "iostream"
#include "vector"
using namespace std;


void foo(vector<int> vec)
{
    for (auto i : vec)
    {
        cout << i << endl;
    }
}

void foo2(std::initializer_list<string> strList)
{
    for (auto s : strList)
    {
        cout << s.c_str() << endl;
    }
}

class Test
{
public:
    int x, y;
    int c;
    char* ptr;
};

int main()
{

    foo({ 1,2,3,4 });

    foo2({ "aa", "bb", "cc"});

    char c = 'a';
    Test t{ 1,2, 30,  &c };

    cout << t.x << "  " << t.y << "  " << t.c << "   " << *t.ptr << endl;

    getchar();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/artisans/article/details/81260295