C ++ language guide (12)-Character Sequence

Source: Internet
Author: User
**************************************** **********************
Link: http://www.cplusplus.com/doc/tutorial/
**************************************** ******
Character Sequence

As you may already know, a powerful function is implemented in the C ++ standard library. String(String) class, which is very useful for controlling and operating character strings. However, because strings are actually character sequences, we can also use common CharTo release them.


Your ad here

For example, the following array:
Char Jenny [20];
Is an array that can store up to 20 char elements. It can be expressed:

Therefore, in this array, We can theoretically store a string of up to 20 characters. But we can also store shorter sequences. For example,JennyThe sequence can be stored somewhere in a program"Hello"Or sequence"Merry ChrismasBecause both of them are less than 20 characters long.

Therefore, because character Arrays can store sequences of less than the total length, a special character is not used to indicate the end of a valid sequence: "null character" (null character ), it is literally written as '/0' (backslash, zero ).

We have 20CharType element is calledJennyArray to store the Character Sequence"HelloAndMerry Christmas"Can be expressed as follows:

Note how to add the end Of the sequence to the end of the sequence NullCharacter ( '/0'). Gray color lattice Representation CharThe element value is unknown. Initialize Null End sequenceBecause character arrays are common arrays, they also follow the same rules as normal arrays. For example, if we want to initialize a character array with some pre-determined characters, we can complete it like other Arrays:
Char myword [] = {'H','E','L','L','O','/0'};
In this case, we declare that there are six CharType element array, and it is initialized as" Hello"And an additional NullCharacter' /0'. However, character arrays are initialized by another method: Using strings. In the expressions we used in the previous section, constants that indicate all strings have appeared multiple times. They are all text enclosed by a pair of double quotes. For example:
"The result is :"
It is a String constant that we may have used. Strings with double quotation marks (") are string constants and their types are actually NullCharacter array. Therefore, a String constant enclosed by double quotation marks is automatically added to the end of the string. NullCharacter ( '/0'Therefore, we can use either of the following two methods to initialize the character array called myword as a null ending sequence:
Char myword [] = {'H','E','L','L','O','/0'};Char myword [] ="Hello";
In both cases, the character array MywordAll are declared as six in the same size. CharArray of elements: Five corresponding words" Hello"Adds the end of a sequence. NullCharacter (' /0'In the second case, double quotation marks ( "), NullCharacters are automatically added. Note: We are discussing the initialization of a character array when it is declared, rather than assigning values to them after they are declared. In fact, because NullThe character array at the end is of the normal type. We have the same restrictions as any other arrays. Therefore, we cannot use a value assignment operation to copy data blocks. Hypothesis MytextIs Char []Type Variable, an expression like this in the source code:
Mystext ="Hello";Mystext [] ="Hello";
Yes InvalidLike the following:
Mystext = {'H','E','L','L','O','/0'};
The reason may be clearer after you have a little more knowledge about pointers, because it will be clear at that time: an array is actually a constant pointer to a memory block. Use Null End Character SequenceCharacter sequences ending with null are common methods for processing strings in C ++, so they are used as in many programs. In fact, a string of this type ( Char []) Is also used in many places. For example, CIN and cout support null-ending sequences as valid character sequences, so they can be directly used to extract strings from CIN or insert strings to cout. For example:

// Null-terminated sequences of Characters# Include <iostream>Using namespace STD; Int main (){Char question [] ="Please, enter your first name :";Char greeting [] ="Hello ,";Char yourname [80];Cout <question;Cin> yourname;

Cout <greeting <yourname <"! ";

Return 0;}

Please, enter your first name: JohnHello, John!
As you can see, we declare a three-character array. The first two are initialized with string constants, but the third is not initialized. In any case, we must specify the size of the array: The first two ( QuestionAnd Greeting) Is defined based on the string constants that initialize them. For YournameThe following is an explicit description: it is 80 characters in size. Char. Finally, the Character Sequence stored in the array can be easily converted to a string object through the value assignment operator:
String mystring;Char myntcs [] ="Some text";Mystring = myntcs;
 

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.