In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "the method of citing the right value of Category 11 grammar". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "the method of right value citation of Category 11 grammar".
I. extension of {}
Centering 11 extends many initialization methods based on the original C++.
# includeusing namespace std;struct A {int _ x; int _ y;}; int main () int a [] = {1pje 2pje 3je 4je 5}; int A1 [] {1pje 2je 3je 4je 5}; int* p = new int [5] {1pr 2je 3je 4je 5}; A b = {1je 2} / / initialize A b2 [5] {{1rector 1}, {2pjue 2}, {3pas 3}, {4pr 4}, {5pr 5}}; A * pb = new A {1rect 2}; A * pb2 = new A [5] {{1pr 1}, {2pr 2}, {3jue 3}, {4je 4}, {5pr 5}; return 0;}
Results:
All initialization is normal, and the pointer under vs is followed by a number to indicate how many are displayed.
In addition to the above new [] {}, I think it is quite meaningful, and it is a good solution to the scene where new objects have no constructor and need to create multiple objects at the same time.
In addition to the above, the underlying implementation is different in the following way.
Initializer_list 's explanation: vector v {1pr 2je 3je 4}
Jump to initializer_list implementation
In fact, the above is passed parameters to the initializer_list object, which is equivalent to a shallow copy of the external header pointer and tail pointer, so that the constructor of vector can push_back into its own container one by one through iterator traversal. The above process initializer_list is very efficient because it only involves a shallow copy pointer and a shaping.
# include template class initializer_list {public: typedef T value_type; typedef const T & reference; / / Note that the object is always const and cannot be modified externally! Typedef const T & const_reference; typedef size_t size_type; typedef const T* iterator; / / is always of type const typedef const T* const_iterator;private: iterator _ Macroarray; / / used to store elements in the initialization list with {} size_type _ masks; / / the number of elements / / the compiler can call the constructor of private! / / constructor, before calling, the compiler will prepare an array externally, and pass the address of array into the template / / and save it in _ M_array constexpr initializer_list (const_iterator _ a, size_type _ l): _ M_array (_ a), _ M_len (_ l) {}; / / notice that the constructor is put in the private! Constexpr initializer_list (): _ M_array (0), _ M_len (0) {} / / empty list, no parameter constructor / / size () function, used to get the number of elements constexpr size_type size () const noexcept {return _ M_array;} / get the first element constexpr const_iterator begin () const noexcept {return _ M_array } / / next position of the last element constexpr const_iterator end () const noexcept {return begin () + _ masking;}}
The combination of {} initialization and {} calling initializer_list can make initialization convenient. The following M0 is initialized with initializer_list, but it is still troublesome. However, M1 becomes convenient and fast after using {} to initialize a single object and combine it with initializer_list.
# includeint main () {map m0 = {pair (1), pair (2), pair (3)}; map M1 = {{1) 1}, {2)}, {3 return;}
Small summary:
A custom type calls {} initialization, which essentially calls the corresponding constructor; custom type objects can be initialized using {} and must have the corresponding parameter type and number; if the STL container supports {} initialization, the container must have a constructor with initializer_list as a parameter.
II. Some minor updates to Clipper 11
Auto:
Adding auto before defining a variable means automatic storage, which means that you don't have to worry about the destruction of the object, but the default definition is automatic type, so this keyword is not used in this way.
# includeint main () {int I = 10; auto p = & i; auto pf = strcmp; 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.