统一初始化是C++ 11中的一项功能,它允许使用一致的语法来初始化从原始类型到集合的变量和对象。换句话说,它引入了花括号初始化,该花括号初始化使用花括号({})来封装初始化程序的值。语法如下:
type var_name{arg1, arg2, ....arg n}
以下是初始化不同类型的不同方法的一些示例:
// uninitialized built-in type
int i;
// initialized built-in type
int j=10;
// initialized built-in type
int k(10);
// Aggregate initialization
int a[]={1, 2, 3, 4}
// default constructor
X x1;
// Parametrized constructor
X x2(1);
// Parametrized constructor with single argument
X x3=3;
// copy-constructor
X x4=x3;
如果使用括号初始化进行初始化,则以上代码可以重写为:
int i{}; // initialized built-in type, equals to int i{0};
int j{10}; // initialized built-in type
int a[]{1, 2, 3, 4} // Aggregate initialization
X x1{}; // default constructor
X x2{1}; // Parametrized constructor;
X x4{x3}; // copy-constructor
统一初始化的应用
动态分配数组的初始化:
CPP
// C++ program to demonstrate initialization
// of dynamic array in C++ using uniform initialization
#include
using namespace std;
int main()
{
// declaring a dynamic array
// and initializing using braces
int* pi = new int[5]{ 1, 2, 3, 4, 5 };
// printing the contents of the array
for (int i = 0; i < 5; i++)
cout << *(pi + i) << " ";
}
CPP
// C++ program to initialize
// an array data member of a class
// with uniform initialization
#include
using namespace std;
class A
{
int arr[3];
public:
// initializing array using
// uniform initialization
A(int x, int y, int z)
: arr{ x, y, z } {};
void show()
{
// printing the contents of the array
for (int i = 0; i < 3; i++)
cout << *(arr + i) <<" ";
}
};
// Driver Code
int main()
{
// New object created and the numbers
// to initialize the array with, are passed
// into it as arguments
A a(1, 2, 3);
a.show();
return 0;
}
CPP
// C++ program to implicitly
// initialize an object to return
#include
using namespace std;
// declaring a class 'A'
class A {
// a and b are data members
int a;
int b;
// constructor
public:
A(int x, int y)
: a(x)
, b(y)
{
}
void show() { cout << a << " " << b; }
};
A f(int a, int b)
{
// The compiler automatically
// deduces that the constructor
// of the class A needs to be called
// and the function parameters of f are
// needed to be passed here
return { a, b };
}
// Driver Code
int main()
{
A x = f(1, 2);
x.show();
return 0;
}
CPP
// C++ program to demonstrate how to
// initialize a function parameter
// using Uniform Initialization
#include
using namespace std;
// declaring a class 'A'
class A {
// a and b are data members
int a;
int b;
public:
A(int x, int y)
: a(x)
, b(y)
{
}
void show() { cout << a << " " << b; }
};
void f(A x) { x.show(); }
// Driver Code
int main()
{
// calling function and initializing it's argument
// using brace initialization
f({ 1, 2 });
return 0;
}
输出
1 2 3 4 5
初始化类的数组数据成员:
CPP
// C++ program to initialize
// an array data member of a class
// with uniform initialization
#include
using namespace std;
class A
{
int arr[3];
public:
// initializing array using
// uniform initialization
A(int x, int y, int z)
: arr{ x, y, z } {};
void show()
{
// printing the contents of the array
for (int i = 0; i < 3; i++)
cout << *(arr + i) <<" ";
}
};
// Driver Code
int main()
{
// New object created and the numbers
// to initialize the array with, are passed
// into it as arguments
A a(1, 2, 3);
a.show();
return 0;
}
输出
1 2 3
隐式初始化对象以返回:
CPP
// C++ program to implicitly
// initialize an object to return
#include
using namespace std;
// declaring a class 'A'
class A {
// a and b are data members
int a;
int b;
// constructor
public:
A(int x, int y)
: a(x)
, b(y)
{
}
void show() { cout << a << " " << b; }
};
A f(int a, int b)
{
// The compiler automatically
// deduces that the constructor
// of the class A needs to be called
// and the function parameters of f are
// needed to be passed here
return { a, b };
}
// Driver Code
int main()
{
A x = f(1, 2);
x.show();
return 0;
}
输出
1 2
隐式初始化函数参数
CPP
// C++ program to demonstrate how to
// initialize a function parameter
// using Uniform Initialization
#include
using namespace std;
// declaring a class 'A'
class A {
// a and b are data members
int a;
int b;
public:
A(int x, int y)
: a(x)
, b(y)
{
}
void show() { cout << a << " " << b; }
};
void f(A x) { x.show(); }
// Driver Code
int main()
{
// calling function and initializing it's argument
// using brace initialization
f({ 1, 2 });
return 0;
}
输出
1 2
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。