C++ basic introduction tutorial (3) : array string structure common body

  • 2020-04-02 02:53:49
  • OfStack

Today's title gets... It's so serious. (xiao ruo: cough!)

Although the content of this chapter is still very detailed (Lao) fine (dao), but has begun to have a lot of content worth recording ~
So, today's first introduction to arrays and strings... And the structure... There are also common.. .

An array of 1.

I remember when I interned in my senior year, I asked my colleague, "what belongs to the Lord?" (actually, something related to the database)
Then the colleague was surprised and said, "oh, you don't even know about arrays.. This, the foundation or want to fill well... Well, what does array mean? It looks like this..."
I was like, "wait, this is an array... I was asking about the Lord..."
Then the colleague breathed a sigh of relief and thought, "oh, I'm scared to death. I thought I had an intern fishing in troubled waters."
 
Wait a minute. I seem to be digressing.
So arrays are so simple that I can't possibly introduce them to you.
Just talk about its declaration and initialization, the following code:


//1. Ordinary people declare and initialize
int nums[3] = {25, 65, 4};
//2. Declare only
int num[3];  //So the values of the elements in the array are unknown
//3. Declare only, then assign
int num[3];
num[0] = 1;
num[1] = 34;
num[3] = 9;
//4. Initialization of literary youth
int nums[3] = {89}; //The 0th element is assigned a value of 89. The rest defaults to 0
int nums[3] = {};   //All elements have a value of 0
//5. Initialization of non-mainstream youth
int nums[] = {1,2,3,4,5}; //Do not specify the size of the array, by the following assignment content to determine the size of the array, the book says not recommended, I do not comment //Initialization of new humans (C++11)
int nums[3] {1, 2, 3}; //The equal sign
is omitted

Most of the cases are already listed in the code
The second kind of article 3 is actually a new feature of C++11.
 
Finally, I personally don't like article 6, which is also new to C++11
This can be unfamiliar to people who are not familiar with C++ (for example, if someone is simply forced to look at a piece of C++ code and the result is this sentence, it may be blindfolded).
In other forms, even those of you who haven't studied C++ can easily see that you're defining an array. (okay, casual)

2. The string

There are two kinds of strings, one is a c-style string, the other is a string.
C-style strings are similar to arrays in the following code:


//1. Initialization of smart people
char name[] = "mutou";
//2. Stupid initialization
char name[6] = "mutou";
//Initialization of neuropathy
char name[6] = {'m', 'u', 't', 'o', 'u', '0'};

This style of string is equivalent to an array of char types, but it ends with a '\0' at the end.

I think, normal people will use the first way (unless special circumstances? I don't know), "mutou" is called a string constant, and it will automatically fill up the array with a '\0' sign.

Then there is the string string, which is used as follows:


//1. Declare and then initialize
std::string name;
name = "mutou";
//Declare and initialize
std::string name = "mutou";
//3. Copy other string contents
std::string name1 = "mutou;
std::string name2 = "hello";
name1 = name2;   //At this point, name1 is "mutou"
name2 = "nihao"; //Now name2 is "nihao" and name1 is still "mutou"
//4. Connection string
std::string name = "mutou";
name += "hehe";

String is in the header string, so use it with #include < The string > .
And it belongs to the namespace STD, so add STD ::
 
String strings are comfortable to use, like they should be
If you use a c-style string, then copying and concatenating strings becomes cumbersome (you need to use the strcpy and strcat functions to do this)

3. Raw string

There's another interesting string called the raw string.
As we all know, strings are surrounded by double quotes, and if you want other double quotes in a string, you use the escape character \.
This can be avoided by using the original string as follows:


std::string name1 = R"("mutou")";
std::string name2 = R"*(("mutou"))*";
std::cout << name1.c_str() << "n";
std::cout << name2.c_str() << "n";

The output results are as follows:


" mutou "
( " mutou " )

Start with R and surround the string with the (and) symbol, so that the middle string can be escaped with characters such as double quotes.
So what about the name2 string? This is to solve the problem of the vicious circle -- how to use extra characters in the original string?
So, we can add a * in the middle of "(add a * in the middle, in)", also add a *, which is like this: R "*(... . Lots of strings in the middle... .) *"
Then you can use (equal characters in the middle of the string.
In fact, you don't have to add a * sign, you can add other things, and can add several, as you like, but there are restrictions, can not add Spaces, open and close brackets, slashes and control characters.

4. Structure

The struct is the precursor of the class
Easy, without further ado, just look:


//Define < br / > struct BadMan
{
    char name[]; //You can also use STD ::string name; However, some compilers may not support
    int age;
    long money;
};
//Use the < br / > BadMan man1 =
{
    "mutou",
    18,
    99999999
};
man1.money = 88888888;

Okay, enough said

5. The appropriate

Have you heard of split personality?
A split personality is when several souls exist in the same body and only one soul controls the body. (xiao ruo: nonsense ~!)
 
C++ also has such a thing, that is the common body, look at the code:


union ManyOne
    {
        int age;
        long money;
        float girl;
    };
    ManyOne mOne;
    mOne.age = 25;
    std::cout << mOne.age << "nn";
    mOne.money = 999990;
    std::cout << mOne.age << "n";
    std::cout << mOne.money << "n";

The output is:


25
999990
999990

A common is very much like a struct, except that you can only have one property in a common.

For example, although age was previously assigned a value of 25, the value of age is not retained once other attributes are assigned.
Because they're using the same space, remember that.

Because all properties of a share use one storage space, the share takes up the memory of the member that needs the most storage space.
 
Maybe people who are hearing about sharing for the first time are confused, but actually sharing can be thought of as a variable with multiple names, and we can use them with different names.

Only, these different names have different types of pendulum.

End of 6.

Well, the book is too detailed (Lao) and the fourth chapter is still under way. Xiao re: who do you think is more nagging than you?


Related articles: