Syntax:
语法:
#include <string> bool operator==(const string& c1, const string& c2); bool operator!=(const string& c1, const string& c2); bool operator<(const string& c1, const string& c2); bool operator>(const string& c1, const string& c2); bool operator<=(const string& c1, const string& c2); bool operator>=(const string& c1, const string& c2); string operator+(const string& s1, const string& s2 ); string operator+(const charT* s, const string& s2 ); string operator+( charT c, const string& s2 ); string operator+( const string& s1, const charT* s ); string operator+( const string& s1, charT c ); string& operator+=(const string& append); string& operator+=(const charT* append); string& operator+=(const charT append); ostream& operator<<( ostream& os, const string& s ); istream& operator>>( istream& is, string& s ); string& operator=( const string& s ); string& operator=( const charT* s ); string& operator=( charT ch ); charT& operator[]( size_type index ); const charT& operator[]( size_type index ) const;
C++ strings can be compared and assigned with the standard comparison operators: ==, !=, <=, >=, <, >, and =. Performing a comparison or assigning one string to another takes linear time.
C++ strings 可以通过标准运算符 ==, !=, <=, >=, <, >, 和 = 进行比较和赋值。一个string和另一个string的比较或赋值操作是线性的。
Two strings are equal if:
两个strings是相等的,当:
Comparisons among strings are done lexicographically.
strings 之间的比较是字典序的。
In addition to the normal container operators, strings can also be concatenated with the + operator and fed to the C++ I/O stream classes with the << and >> operators.
除了普通容器支持的运算符,strings 也可以通过 + 运算符进行连接,同时它也通过<< 和 >> 支持C++的输入/输出流。
For example, the following code concatenates two strings and displays the result:
例如,如下所示代码连接两个strings并显示其结果:
string s1 = "Now is the time..."; string s2 = "for all good men..."; string s3 = s1 + s2; cout << "s3 is " << s3 << endl;
Furthermore, strings can be assigned values that are other strings, character arrays, or even single characters. The following code is perfectly valid:
更进一步,strings 可以通过其它strings,字符数组,甚至是单个字符进行赋值。例如,以下代码是合法的:
char ch = 'N'; string s; s = ch;
Individual characters of a string can be examined with the [] operator, which runs in constant time.
可以通过 [] 运算符检索string的单个字符,其复杂度是固定的