In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The purpose of this article is to share with you about how to use the initialization list initializer lists in Clippers 11. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Initialization lists are introduced to initialize variables and objects. Custom types, including initializer_list header files if you want to use the initialization list.
Cantilever 11 uses the initialization of curly braces (list initialization) as a generic initialization method that can be used for all types. The initialization list is not implicitly converted.
The new type provided by Category 11 is defined in the header file.
Template
< class T >Class initializer_list
Let's talk about its use first, and then introduce it in detail.
First of all, after you have initializer_list, it is much easier to initialize the container of STL, for example, you used to initialize a vector like this:
Int a [] = {0,1,2,3}
Std::vector vec (a, a+sizeof (a))
Or
Std::vector vec
Vec.push_back (1)
Vec.push_back (3)
Vec.push_back (3)
Vec.push_back (2)
Once you have initializer_list, you can just initialize the array:
Class Test {private: static std::map const nameToBirthday = {{"lisi", "18841011"}, {"zhangsan", "18850123"}, {"wangwu", "18870908"}, {"zhaoliu", "18810316"},};}
Of course, the std::map in it must provide a constructor with an argument of initializer_list, such as:
Map (std::initializer_list init
Const Compare& comp = Compare ()
Const Allocator& alloc = Allocator ()
In fact, in for (initializer: list), if list is shaped like: {a, b, c.}, then list is automatically constructed into an initializer_list object.
Let's introduce initializer_list a little bit.
An initializer_list is automatically constructed when it occurs in the following two situations:
Initialization is initialized with curly braces and is constructed automatically. Including function calls and assignments
When it comes to for (initializer: list), list is automatically constructed as an initializer_list object
That is, initializer_list objects can only be initialized with curly braces {}.
Copying an initializer_list object does not copy the elements in it. Actually, it's just a quote. And all the elements in it are const.
The following example can help us better understand how to use initializer_list:
# include # include # include using namespace std;template struct S {vector v; S (initializer_list l): v (l) {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.