首页 > 代码库 > 2-2. Initializing Objects with Initializer Lists

2-2. Initializing Objects with Initializer Lists

Using Uniform Initialization to Construct a vector

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    using MyVector = vector<int>;
    MyVector vectorA( 1 );
    cout << vectorA.size() << " " << vectorA[0] << endl;
    MyVector vectorB( 1, 10 );
    cout << vectorB.size() << " " << vectorB[0] << endl;
    MyVector vectorC{ 1, 10 };
    cout << vectorC.size() << " " << vectorC[0] << endl;
    return 0;
}

技术分享

1.使用Uniform Initialization可以使得初始化比较方便。

2.using的使用也比较陌生

 

2-2. Initializing Objects with Initializer Lists