A char equivalent of C [], C + + adds a new class--string.
You must add a header file using the string type #include <string>
Using Std::string; The next steps are some of the things that are commonly used by string.
There is no significant difference between the definition of a string type and the definition of a base type. More of this definition pattern, string S1 (10,c); Equivalent to string s1 = "CCCCCCCCCC";
Action on a String object
Some operations of string
Getline (IS, s) reads a row from IS and assigns it to S, returning is;
S.empty () Returns TRUE if S is null, otherwise returns false;
S.size () returns the number of characters in S
S1+S2 returns results after S1 and S2 are connected
Note: When you read the contents of a standard input into S, the string object automatically ignores the whitespace at the beginning (that is, whitespace, newline characters). tab, etc.) and start reading from the first real character until you meet the next blank. For example, when you enter "Erhuol fine!", the string object reads only "Erhuol"
So, what do we do when we need to read a whole line of characters?
Getline, we can use this function as follows:
string S1; Header file namespace omitted
Getline (CIN, S1);
This allows the processor to read all of the preceding newline characters (including newline characters) to S1, but does not save the newline character when it is saved. (This indicates that the line break that triggered the return of the Getline function was actually discarded).
However, it is worth noting thatGetline ends the read operation and returns the result whenever a newline character is encountered, even if the input is the beginning of a newline character . Cases:
Hi!erhuol.
The resulting input would be an empty string.
In particular, here is a description of the comparison size between two string objects, in terms of (case-sensitive) dictionary order :
1. If the length of the two string objects is inconsistent and each character of the shorter object is the same as the character at the corresponding position of the longer object, the shorter string object is less than the longer string object.
2. If two string objects are not equal, the result of the first comparison of the different characters in the two objects.
For the size function in the previous operation
What it returns is a String::size_type , which is a matching type that embodies the standard library type and machine-independent features, paying particular attention to the fact that this is an unsigned value and enough to hold the size of any string object . Primitive types we know that mixing int and unsigned type values can result in unpredictable results, so if an expression already has the size () function, do not use int again to avoid the potential problems of mixing .
Example: Auto len = S1.size (); Len is a value of type String::size_type.
Although the specifics are not known, this value can be used to perform operations like while (len--).
Working with characters in a string object
1. We can use the functions in the Cctype header file to determine the characteristics of one of the characters, for example: Isalnum (c) is true when C is a letter or a number.
2. Use the for (auto c:s1) to traverse the entire string object. Tip: C Here is a Char object, which is a typedef char in vs2015 ....
3. Use the subscript [] as an array to access it. e.g. S1[i] = ' x '; When S1 is empty, then s1[0] is defined.
string of C + +