a class type, variable-length sequence of characters.

Define and Initialize

the class defines several ways:

string s1;           // Default initialize; s1 is empty string
string s2 = s1;      // copy initialize
string s3 = “hiya”;  // copy initialize
string s4(10, ‘c’);  // direct initialize

Operations

operatorseffect
>, >=, <, , ==compare
=assign
+两边至少一个要是string,因此返回是个string;没有交换律
[]takes a string::size_type, so convert to unsigned before use, can takes an expression yields an integral value; returns a reference;

Functions

functionseffect
istream getline(istream, string)the ‘\n’ is discarded
🔗cctypefunctions of processing characters in header
member funcseffect
bool empty()true if is not empty
string::size_type size()always auto len = line.size(); is an unsigned
iterator funcs🔗 Iterators
string str;
str.find(,)

Mixing Library strings and C-Style Strings

新的包容了旧的,但旧的不能回去变新的。

char cs[] = “Hello”;
string s = cs;          // ok
char *cs1 = s;          // error
char *cs2 = s.c_str();  // ok