String is a string class in the C ++ standard class. It can be used to replace C-style string operations to reduce errors caused by underlying factors such as pointers.
Use the string class,
Header file reference
# I nclude <iostream>
# I nclude <string. h>
# If _ msc_ver> 1020
Using namespace STD;
# Endif
Specifically, the STD namespace must be referenced.
Definition
String STR;
String STR = "ABCD ";
String STR ("ABCD ");
String str2 (STR); // use another string object to construct
* Can be directly compared
If (STR = str2)
* Operations such as ++ and ++ can be used.
String str3 = STR + str3;
Str3 + = STR;
* C-style strings can be automatically converted to the string type.
Const char * Pc = ",";
String str4 = STR + PC + str2;
But this is not the case.
Char * c_str = STR; // Error
Char * c_str = Str. c_str (); // This is also incorrect, because to prevent operations on the string content directly, the const is returned.
Const char * c_str = Str. c_str (); // OK
* Size () returns the length, excluding the Terminator.
* Empty () can be used to directly determine whether the string is null.
* Single element access
Cout <STR [2] <Endl;
* Iterative operations
For (string: iterator it = Str. Begin (); it! = Str. End (); It ++)
{
Cout <* It <Endl;
}