目录
Non-member function overloads(非成员函数重载)
String operations(字符串运算)
C_str:
接口:
const char* c_str() const;
返回一个指向数组的指针,该数组包含一个以空结尾的字符序列(即C-string),表示字符串对象的当前值。
该数组包括组成字符串对象值的相同字符序列,以及在末尾附加的终止空字符('\0')
void Test()
{
string str("hello world");
char* cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
cout << cstr << endl;
}
copy:
接口:
size_t copy (char* s, size_t len, size_t pos = 0) const;
将字符串对象当前值的子字符串复制到s所指向的数组中。该子字符串包含从位置pos开始的len个字符。
该函数不会在复制内容的末尾附加空字符s->要复制的对象的指针
len->要复制的字符的个数
pos->复制的起始位置(如果超出对象长度会抛异常)
void Test02()
{
string str("hello world");
char buffer[20];
size_t len = str.copy(buffer, 2, 3);
cout << len << endl;
buffer[len] = '\0';
cout << buffer << endl;
}
find:
接口:
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (char c, size_t pos = 0) const;
在字符串中搜索由其参数指定的序列的第一次出现。
当指定pos时,搜索只包括位置pos或位置pos之后的字符,忽略任何可能出现的包含位置pos之前字符的字符。
void Test03()
{
string str("hello world");
string str1("hello");
char arr[] = "hello";
size_t a = str.find(str);
size_t a = str.find(arr);
size_t a = str.find('h');
cout << a << endl;
}
find_first_of:
size_t find_first_of (const string& str, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos = 0) const;
size_t find_first_of (char c, size_t pos = 0) const;
在字符串中搜索与参数中指定的任何字符匹配的第一个字符
当指定pos时,搜索只包括位置pos处或位置pos之后的字符,忽略位置pos之前可能出现的任何字符
void Test04()
{
string str("hello world");
string str1("abcde");
size_t b1 = str.find_first_of("abcde");
size_t b2 = str.find_first_of(str1);
size_t b3 = str.find_first_of('o');
cout << b1 << endl;
cout << b2 << endl;
cout << b3 << endl;
}
find_last_of:
size_t find_last_of (const string& str, size_t pos = 0) const;
size_t find_last_of (const char* s, size_t pos = 0) const;
size_t find_last_of (char c, size_t pos = 0) const;
在字符串中搜索与参数中指定的任何字符匹配的最后一个字符。
当指定pos时,搜索只包括位置pos处或位置pos之前的字符,忽略位置pos之后可能出现的任何字符
void Test05()
{
string str("hello world");
string str1("abcde");
size_t b1 = str.find_last_of("abcde");
size_t b2 = str.find_last_of(str1);
size_t b3 = str.find_last_of('o');
cout << b1 << endl;
cout << b2 << endl;
cout << b3 << endl;
}
Non-member function overloads(非成员函数重载)
operator+:
接口:
string operator+ (const string& lhs, const string& rhs)
string operator+ (const string& lhs, const char* rhs);
string operator+ (const string& lhs, char rhs);
返回一个新构造的字符串对象,其值是 lhs 中字符的串联,后跟 rhs 的字符
void Test06()
{
string str1("hello");
string str2("world");
char arr1[] = "11111";
string str3 = str1 + str2;
string str4 = str1 + arr1;
cout << str3 << endl;
cout << str4 << endl;
}
relational operators:
bool operator== (const string& lhs, const string& rhs);
bool operator>= (const string& lhs, const string& rhs);
bool operator<= (const string& lhs, const string& rhs);
bool operator > (const string& lhs, const string& rhs);
bool operator < (const string& lhs, const string& rhs);
bool operator != (const string& lhs, const string& rhs);
在字符串对象 lhs 和 rhs 之间执行适当的比较操作
void Test07()
{
string str1("hello");
string str2("world");
bool a1 = str1 > str2;
bool a2 = str1 < str2;
bool a3 = str1 <= str2;
bool a4 = str1 >= str2;
bool a5 = str1 == str2;
bool a6 = str1 != str2;
cout << a1 << endl;
cout << a2 << endl;
cout << a3 << endl;
cout << a4 << endl;
cout << a5 << endl;
cout << a6 << endl;
}
getline:
istream& getline (istream& is, string& str)
从 is 中提取字符并将它们存储到 str 中,直到找到分隔符 dim
如果在 is 中到达文件末尾,或者在输入操作期间发生其他错误,则提取也会停止
如果找到分隔符,则将其提取并丢弃(即不存储分隔符,并在其之后开始下一个输入操作)
void Test08()
{
string str;
getline(cin, str);
cout << str << endl;
}