C++11 new Features--initialization list initializer_list

Source: Internet
Author: User
Tags acos

Breaking water:

Because the recent data structure has an experimental report that is to sort the string, want to steal a lazy do not want to assign a value, although you can use strcpy and passed in the form of a two-level pointer to write directly, but this feeling beautiful.

And then I went to the film. C++11 's new feature-initialization list, the concept is not said, just tell me how to use it, it is normal to write a constructor, and then change the parameters to initializer_list< data type > &t

It can be understood that the incoming parameter data is put into a storage T, using the C++11 auto can directly traverse the memory T, and then the traversed value to the structure. Here is a char array instead of int because there is a problem, if you change the type of initializer_list angle brackets to char *, you will get an error, because the ordinary "" double quotation marks represent the actual type of the literal, the actual itself is a pointer, Point to the address in the Global const variable, so const *a= "123", *b= "123", print A and B address will find A and B is the same, the morning is because this place Baidu for a while to understand, another point is if the declaration is an info array, Then it is obvious that the instance object of info is initialized, so the string is also enclosed in curly braces to form an info instance object.

Here's the full code:

#include <stdio.h> #include <bits/stdc++.h>using namespace std; #define INF 0x3f3f3f3f#define LC (x) (x< <1) #define RC (x) ((x<<1) +1) #define MID (x, y) ((x+y) >>1) #define CLR (Arr,val) memset (arr,val,sizeof (arr) ) #define FAST_IO Ios::sync_with_stdio (false); Cin.tie (0); typedef pair<int, int> pii;typedef Long Long ll;const Double PI = ACOs ( -1.0), struct Info{char name[20];info () {}info (const initializer_list<const char *> &t) {for (AU To &item:t) strcpy (name, item);}}; Info One[2] = {"This was one"}, {"This is a"}};int main (void) {for (auto &it:one) cout << it.name << End L;return 0;}

The above is initialized directly with a const pointer, and if I already have a two-dimensional array of strings like this: char s[maxn][maxn] = {"1", "2", "3", "4"}, how do you assign a value? It is obvious that the address of the first address of each string is passed in.

#include <stdio.h> #include <bits/stdc++.h>using namespace std; #define INF 0x3f3f3f3f#define LC (x) (x< <1) #define RC (x) ((x<<1) +1) #define MID (x, y) ((x+y) >>1) #define CLR (Arr,val) memset (arr,val,sizeof (arr) ) #define FAST_IO Ios::sync_with_stdio (false); Cin.tie (0); typedef pair<int, int> pii;typedef Long Long ll;const Double PI = ACOs ( -1.0); const int N = 20;char S[n][n] = {"This was one", "This is one", "3", "4"};struct Info{char name[20]; Info () {}info (const initializer_list<const char *> &t) {for (auto &item:t) strcpy (name, item);}}; Info One[2] = {{S[0]}, {S[1]}};int main (void) {for (auto &it:one) cout << it.name << endl;return 0;}

C++11 new Features--initialization list initializer_list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.