代码如下:
#include <iostream>
using namespace std;void func(initializer_list<int> ls)
{auto it = ls.begin();for (; it != ls.end(); it++){cout << *it << " ";}cout << endl;
}int main()
{func({ 1,2,5,12,23 });return 0;
}
测试结果:
代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;class Test
{
public:Test(initializer_list<string> list){for (auto it = list.begin(); it != list.end(); it++){cout << *it << " ";name.push_back(*it);}cout << endl;}private:vector<string> name;
};int main()
{Test t({ "Tom","jACK","MIKE" });return 0;
}