stay C++11 in ,vector This syntax appears for initialization and equal sign assignment of
vector<int> nums({1, 2, 3, 4, 5}); vector<int> nums3 = vector({1, 2, 3, 4,
5}); vector<int> nums2; nums2 = {1, 2, 3, 4, 5};
And in the C++11 before , We can only :,
int a[5] = {1, 2, 3, 4, 5}; vector<int> nums4(a); // sorry , Nonexistent vector<int>
nums5(a, a+5); // That's about it
See this C++11 I'm curious how it works , We are familiar with array initialization vector To do this, you must specify the first and last addresses of the array . Because during the operation period , You can't tell the length of an array
, The array length is known only at compile time .

It turns out that C++11 This method of initialization is through the standard library initializer_list Realized .


Initialization with two layers of curly braces , The first layer of curly brackets is separated by commas :
vector<vector<char>> board =
{{'X','.','.','X'},{'.','.','.','X'},{'.','.','.','X'}}; vector<vector<string>>
letter({ {"a","b","c"},{ "d","e","f" },{ "g","h","i" }, { "j","k","l" },{
"m","n","o" }, { "p","q","r" ,"s" }, { "t","u","v" },{ "w","x","y" ,"z"} });

It's more than initialization

C++11 in , You can also assign values using a list of values surrounded by braces

Let's review the initialization of variables :

vector vi{1, 2, 3, 4, 5};

Next is the case of assignment :

vector vi;

vi = {6, 7, 8, 9, 10};

This form , It is very useful for the assignment of a finite number of values .


If the initialization list in braces is empty , The compiler creates a value initialized temporary value and assigns it to the assignment object .
vector<string> ret; if (digits.size() == 0){ ret = {}; return ret; }
In the example above, an empty vector is returned !!!

©2020 ioDraw All rights reserved
Huawei Hongmeng system learning notes 9- Ecological construction of developers JavaScript Do a simple guess number games Centralized processing (mean centering) The myth and truth of JS Experimental summary of basic key knowledge ( whole ) Detailed explanation PHP Medium die,exit,returnPython Student information management system ( Lite )re Common methods of modules stata Basic operation of linear regression analysis flutter study -- Search box Docker Import of containers and mirrors , export