I also want to learn C ++ _ Chapter 3: strings

Source: Internet
Author: User

 

At the same time as conquering the programming art, it can also involve other artistic forms in many aspects! The happiness of a person lies in emotion, and the expression of emotion becomes art. From now on, I recommend a song at the end of each study note. It was classic at the time! So that every day's happiness can add another 1 point! (Today is the pride of Chinese musicians: Li kangmin) I have used strings in my previous study notes! Enclose it in quotation marks and output it! But it seems monotonous. Is there a way to make strings more colorful ?! The following is an example:

This code is output from cout <and \ n. I also know that it is a line feed. There is a \ t in the middle. I knew it when I was learning C. This is a tab. In this way, I can guess the program running result smoothly. The result is: Shape Sides ----- Square 4 Circle? Check the correct results and see if they are the same!

Haha! Exactly.

What if double quotation marks are printed in the string ?! I tried to write the code: std: cout <"\" \ n; completely correct! A backslash can be used to convert a special character into a normal character. Code that will go wrong:

 

This code, I thought it would be wrong! Because at the end of row 11th, I didn't write the output operator at the beginning of line 12th! The result is still the correct result in the output:

 

Success! This is strange! Oh! In general, we use output operators to separate each output item. However, if there are more than two adjacent strings, the compiler will automatically combine them into a single string. But remember, this technique is only applicable to strings and not to characters! Let's see what the character is used:

 

Therefore, when outputting a string, you can select multiple styles and output the same result. You can choose your own style as needed, but it is best to keep a style to the end!

There is another problem here. Why not write it like this? Use a large string to include numbers, for example:

This looks like a little more informative! However, if I do not know the name and number of edges in advance, this may cause a problem, it is very likely to read the value stored in the variable without knowing it, such as the Code

Hey! Good! The C ++ string has been built in. It does not need to be like C. It is not built in, but C is also very easy to use. The String string type is used here, so the header file 'string' must be included above, because you don't want to tell the compiler that I want to use 'string'. the compiler is not prepared. It is a little nervous when you see 'string, no! Just like a person who does not know that he is in a crush will pass through this intersection. As a result, he is a little nervous when he sees him at the intersection without preparation! In the above program, the value "Triangle" is assigned to the string ". So how to assign a null value? Let me first think about it. If it is null, there are several ideas: 1. Double quotation marks are written in brackets, which are empty! 2. Don't use parentheses. Apply directly. 3. directly give NULL. Int sides. I don't have any value! First try :...... Result: The idea of string 1 and 2 reaches the goal, and 3 does not work if NULL is given directly. In addition, int sides does not initialize random values. So how is writing the most secure and safe statement ?! Write std: string empty (""); int zero (0); I have to remember it! In the future, I want to correct one habit. I will initialize each variable later, even if I know that the program will soon rewrite it, such as the input loop in my previous code.

Dragon power from London: (style: Speed metal)

 

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.