C + + Basics Getting Started Tutorial (iii): array, string, struct, shared body _c language

Source: Internet
Author: User
Tags arrays control characters

Today's title gets ... A very serious feeling. (Jor: Cough puff)

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 the first time to introduce arrays and strings ... As well as the structural body. There are also shared bodies. it.

1. Array

I remember when the senior internship, ask colleagues: "What is the owner?" "(actually something related to the database)
Then the colleague said in surprise: "Ah, you don't even know the array." This, the foundation still need to make a good supplement ... Oh, what the array means, is this ... "
I listen, it's not right, "Wait, this is an array ... Actually, I was asking this owner ...
Then the colleague breathed a sigh of relief, estimated in the mind, "Alas, scared me to death, I thought that came to a troubled intern."

Wait, I seem to be off topic.
So, the array is so simple, I can not introduce to you.
Let's say its declaration and initialization, the following code:

Copy Code code as follows:

1. Declaration and initialization of ordinary people
int nums[3] = {25, 65, 4};
2. Declare only
int num[3]; So the element values in the array are unknown.
3. Declare only and then assign a value
int num[3];
Num[0] = 1;
Num[1] = 34;
NUM[3] = 9;
4. Initialization of literary and artistic youth
int nums[3] = {89}; The No. 0 element is assigned a value of 89, and the other 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, by the subsequent assignment content to determine the size of the array, the book said not recommended, I do not comment ~
6. Initialization of new humans (C++11)
int nums[3] {1, 2, 3}; The Equals sign is omitted

Most of the information has been listed in the code.
The second of the 3rd article is actually a new feature of c++11.

Finally, for the 6th of this kind of words, is also c++11 new things, I personally do not like ~
This may make unfamiliar C + + people feel very unfamiliar (for example, others just forced to see a section of C + + code, the result came out this sentence, may be blindfolded).
Other forms, even those who have not learned C + +, can easily know that it is defining an array. (OK, let's talk about it)

2. String

There are two types of strings, one is a C-style string, the other is a string (which can be said to be a class)
C-style strings and arrays are very similar to the following code:

Copy Code code as follows:

1. The initialization of the Wise
Char name[] = "Mutou";
2. Initialization of Fools
Char name[6] = "Mutou";
3. The initialization of snake sperm disease
Char name[6] = {' m ', ' u ', ' t ', ' o ', ' u ', ' n '};

This style of string is equivalent to an array of char types, but it needs to add a ' yes ' at the end as a terminator (all that has been learned C knows LA).

I think that normal people will use the first way (unless special circumstances?). I don't know, "Mutou" is called a string constant, and then the array is automatically filled with a ' yes ' sign.

followed by string strings, using the following method:

Copy Code code as follows:

1. Declare first, then initialize
std::string name;
Name = "Mutou";
2. Declaration and initialization
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"; At this point name2 is "Nihao" and name1 is still "Mutou"
4. Connection string
std::string name = "Mutou";
Name = + "hehe";

String is in the header file string, so #include <string> should be added when used.
And it belongs to the namespace Std, so you also need to add std::

String strings are comfortable to use, just like they should be.
If you use a C-style string, copying strings and connection strings can become cumbersome (you need to use the strcpy and strcat functions to do so)

3. Original string

There is also a more interesting string, called the original string.
As we all know, strings are enclosed in double quotes, and if you want to have other double quotes in the string, use the escape character \.
Using the original string can avoid this hassle, such as the following code:

Copy Code code 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:

Copy Code code as follows:

"Mutou"
("Mutou")

Using R to begin with, and then surround the string with the "(ampersand)" symbol, the middle string can use double quotes and other characters that are supposed to be escaped.
So what about the name2 string? This is to solve the vicious circle problem-how to use the extra characters in the original string?
So, we can add a * in the middle of "(add a *, in)", which means: "R" * (...) Many strings in the middle ...) * "
It can then be used in the middle of the string (such as characters.)
In fact not necessarily add * number, can add other things, and can add several, as you like, but also have restrictions, can not add space, left and right brackets, slashes and control characters.

4. Structural body

The structure should be regarded as the predecessor of the class.
Relatively simple, not much said, casually look at:

Copy Code code as follows:

Defined
struct Badman
{
Char name[]; You can also use std::string name; But some compilers may not support
int age;
Long money;
};
Use
Badman Man1 =
{
"Mutou",
18,
99999999
};
Man1.money = 88888888;

Well, not much to say ~

5. Common body

Have you ever heard of a split personality?
You've never heard of it. Personality split is a good few people's souls exist in the same body, and only one soul can control the body. (Jor: Nonsense ~! )

C + + also has this kind of thing, that is the common body, look at the code:

Copy Code code as follows:

Union ManyOne
{
int age;
Long money;
float girl;
};
ManyOne Mone;
Mone.age = 25;
Std::cout << mone.age << "\ n";
Mone.money = 999990;
Std::cout << mone.age << "\ n";
Std::cout << Mone.money << "\ n";

The output results are:

Copy Code code as follows:

25
999990
999990

The common body and the struct are very similar, the only difference is that the properties in the shared body can only exist at the same time.

For example, just the code, although the age is assigned to 25 before, but once the other attributes are assigned, then the value of age will not be retained.
Because they are using the same space, remember that.

Because all of the properties of a common body use one storage space, the total size of the shared body is based on the member who needs the maximum storage space.

Perhaps the first time people who have heard of the shared body will be confused, in fact, the common body can be imagined as a variable has multiple names, we can use them with different names.

It's just that the different names are not the same type of pendulum.

6. End

Well, this book is really too detailed (Lao) (DAO), and the fourth chapter is continuing. (Jor: Who do you think is more talkative than you?) )

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.