本文共 12048 字,大约阅读时间需要 40 分钟。
C++标准库中的string类是处理字符串操作的高效工具,支持多种字符串操作,包括构造、插入、拼接、删除、查找、比较、替换、交换等功能。以下从多个方面详细介绍string类的使用方法。
string类支持多种构造方式以满足不同场景的需求,常用的构造函数包括:
string();
string(const char* s);
s所指的字符序列。string(const char* s, size_t n);
s所指字符序列的前n个字符。string(size_t n, char c);
n个c字符的字符串。string(const string& str);
str的复制品。string(const string& str, size_t pos, size_t len = npos);
str中从pos开始并跨越len个字符的部分。string s1; // 构造空字符串string s2("hello string"); // 复制"hello string"string s3("hello string", 3); // 复制"hello string"的前3个字符string s4(10, 's'); // 生成10个's'字符的字符串string s5(s2); // 生成s2的复制品string s6(s2, 0, 4); // 复制s2中从字符位置0开始并跨越4个字符的部分 push_back函数用于在字符串末尾插入一个字符:
#include#include using namespace std;int main() { string s; s.push_back('C'); s.push_back('S'); s.push_back('D'); s.push_back('N'); cout << s << endl; // 输出"CSDN" return 0;}
insert函数用于在指定位置插入字符、字符串或字符数组:
#include#include using namespace std;int main() { string s("C"); // insert(pos, str)在pos位置插入字符串str s.insert(1, "S"); // "CS" // insert(pos, string)在pos位置插入string对象 string t("D"); s.insert(2, t); // "CSD" // insert(pos, char)在pos位置插入字符char s.insert(s.end(), 'N'); // "CSDN" cout << s << endl; // 输出"CSDN" return 0;}
使用append函数完成字符串的拼接:
#include#include using namespace std;int main() { string s1("I"); string s2(" like"); // append(string)完成两个string对象的拼接 s1.append(s2); // "I like" // append(str)完成string对象和字符串str的拼接 s1.append(" C++"); // "I like C++" // append(n, char)将n个字符char拼接到string对象后面 s1.append(3, '!'); // "I like C++!!!" cout << s1 << endl; // 输出"I like C++!!!" return 0;}
pop_back函数用于删除字符串末尾的字符:
#include#include using namespace std;int main() { string s("C++"); s.pop_back(); // "C" s.pop_back(); // "C" cout << s << endl; // 输出"C" return 0;}
erase函数用于删除字符串中的指定部分:
#include#include using namespace std;int main() { string s("I like C++!!!"); // erase(pos, n)删除pos位置开始的n个字符 s.erase(8, 5); // "I like C" // erase(pos)删除pos位置的字符 s.erase(s.end() - 1); // "I like" // erase(pos1, pos2)删除[pos1, pos2)上所有字符 s.erase(s.begin() + 1, s.end()); // "I" cout << s << endl; // 输出"I" return 0;}
find函数用于查找字符串中第一个匹配项:
#include#include using namespace std;int main() { string s1("http://www.cplusplus.com/reference/string/string/find/"); // find(string)正向搜索与string对象所匹配的第一个位置 string s2("www"); size_t pos1 = s1.find(s2); // 输出7 // find(str)正向搜索与字符串str所匹配的第一个位置 char str[] = "cplusplus.com"; size_t pos2 = s1.find(str); // 输出11 // find(char)正向搜索与字符char所匹配的第一个位置 size_t pos3 = s1.find(':'); // 输出4 cout << pos1 << endl << pos2 << endl << pos3 << endl; // 输出"7 11 4" return 0;}
rfind函数用于查找字符串中第一个反向匹配项:
#include#include using namespace std;int main() { string s1("http://www.cplusplus.com/reference/string/string/find/"); // rfind(string)反向搜索与string对象所匹配的第一个位置 string s2("string"); size_t pos1 = s1.rfind(s2); // 输出42 // rfind(str)反向搜索与字符串str所匹配的第一个位置 char str[] = "reference"; size_t pos2 = s1.rfind(str); // 输出25 // rfind(char)反向搜索与字符char所匹配的第一个位置 size_t pos3 = s1.rfind('/'); // 输出53 cout << pos1 << endl << pos2 << endl << pos3 << endl; // 输出"42 25 53" return 0;}
compare函数用于比较两个字符串:
#include#include using namespace std;int main() { string s1("hello world"); string s2("hello CSDN"); // 比较"hello world"和"hello CSDN" cout << s1.compare(s2) << endl; // 输出1 // 比较"ell"和"hello CSDN" cout << s1.compare(1, 3, s2) << endl; // 输出-1 // 比较"hello"和"hello" cout << s1.compare(0, 4, s2, 0, 4) << endl; // 输出0 return 0;}
replace函数用于替换字符串中的部分内容:
#include#include using namespace std;int main() { string s("hello world"); // replace(pos, len, str)将pos位置开始的len个字符替换为字符串str s.replace(6, 4, "CSDN"); // "hello CSDNd" // replace(pos, len, n, char)将pos位置开始的len个字符替换为n个字符char s.replace(10, 1, 3, '!'); // "hello CSDN!!!" cout << s << endl; // 输出"hello CSDN!!!" return 0;}
swap函数用于交换两个string对象的内容:
#include#include using namespace std;int main() { string s1("hello"); string s2("CSDN"); // 使用string类的成员函数swap交换s1和s2 s1.swap(s2); cout << s1 << endl; // 输出"CSDN" cout << s2 << endl; // 输出"hello" // 使用非成员函数swap交换s1和s2 swap(s1, s2); cout << s1 << endl; // 输出"hello" cout << s2 << endl; // 输出"CSDN" return 0;}
#include#include using namespace std;int main() { string s("CSDN"); cout << s.size() << endl; // 输出4 cout << s.length() << endl; // 输出4 return 0;}
#include#include using namespace std;int main() { string s("CSDN"); cout << s.max_size() << endl; // 输出4294967294 return 0;}
#include#include using namespace std;int main() { string s("CSDN"); cout << s.capacity() << endl; // 输出15 return 0;}
#include#include using namespace std;int main() { string s1("CSDN"); // resize(n) n大于当前size时,扩大到n,默认填充'\0' s1.resize(20); cout << s1 << endl; // 输出"CSDN" cout << s1.size() << endl; // 输出20 cout << s1.capacity() << endl; // 输出31 string s2("CSDN"); // resize(n, char) n大于当前size时,扩大到n,填充指定字符 s2.resize(20, 'x'); cout << s2 << endl; // 输出"CSDNxxxxxxxxxxxxxxxx" cout << s2.size() << endl; // 输出20 cout << s2.capacity() << endl; // 输出31 // resize(n) n小于当前size时,缩小到n s3.resize(2); cout << s3 << endl; // 输出"CS" cout << s3.size() << endl; // 输出2 cout << s3.capacity() << endl; // 输出15 return 0;}
#include#include using namespace std;int main() { string s("CSDN"); // reserve(n) n大于当前capacity时,扩大到n或更大 s.reserve(20); cout << s << endl; // 输出"CSDN" cout << s.size() << endl; // 输出4 cout << s.capacity() << endl; // 输出31 // reserve(n) n小于当前capacity时,无效果 s.reserve(2); cout << s << endl; // 输出"CSDN" cout << s.size() << endl; // 输出4 cout << s.capacity() << endl; // 输出31 return 0;}
#include#include using namespace std;int main() { string s("CSDN"); s.clear(); cout << s << endl; // 输出空字符串 return 0;}
#include#include using namespace std;int main() { string s("CSDN"); cout << s.empty() << endl; // 输出0 s.clear(); cout << s.empty() << endl; // 输出1 return 0;}
#include#include using namespace std;int main() { string s("CSDN"); // []+下标访问元素 for (size_t i = 0; i < s.size(); i++) { cout << s[i]; } cout << endl; // 输出"CSDN" // 修改元素 for (size_t i = 0; i < s.size(); i++) { s[i] = 'x'; } cout << s << endl; // 输出"xxxx" return 0;}
#include#include using namespace std;int main() { string s("CSDN"); // at(pos)访问pos位置的元素 for (size_t i = 0; i < s.size(); i++) { cout << s.at(i); } cout << endl; // 输出"CSDN" // 修改元素 for (size_t i = 0; i < s.size(); i++) { s.at(i) = 'x'; } cout << s << endl; // 输出"xxxx" return 0;}
#include#include using namespace std;int main() { string s("CSDN"); // 使用范围for访问元素 for (auto e : s) { cout << e; } cout << endl; // 输出"CSDN" // 使用范围for修改元素,e必须是引用类型 for (auto& e : s) { e = 'x'; } cout << s << endl; // 输出"xxxx" return 0;}
#include#include using namespace std;int main() { string s("CSDN"); // 使用迭代器访问元素 string::iterator it1 = s.begin(); while (it1 != s.end()) { cout << *it1; it1++; } cout << endl; // 输出"CSDN" // 修改元素 string::iterator it2 = s.begin(); while (it2 != s.end()) { *it2 += 1; it2++; } cout << s << endl; // 输出"DTEO" return 0;}
string类对=运算符进行了重载,支持多种赋值方式:
#include#include using namespace std;int main() { string s1; string s2("CSDN"); // string类赋值 s1 = s2; // "CSDN" // 字符串赋值 s1 = "hello"; // 字符赋值 s1 = 'x'; cout << s1 << endl; // 输出"x" return 0;}
string类对+=运算符进行了重载,支持多种复合赋值方式:
#include#include using namespace std;int main() { string s1; string s2("hello"); // string类复合赋值 s1 += s2; // "hello" // 字符串复合赋值 s1 += " CSDN"; // "hello CSDN" // 字符复合赋值 s1 += '!'; // "hello CSDN!" cout << s1 << endl; // 输出"hello CSDN!" return 0;}
string类对+运算符进行了重载,支持多种字符串操作:
#include#include using namespace std;int main() { string s; string s1("super"); string s2("man"); char str[] = "woman"; char ch = '!'; // string类 + string类 s = s1 + s2; // "superman" // string类 + 字符串 s = s1 + str; // "superwoman" // 字符串 + string类 s = str + s1; // "womansuper" // string类 + 字符 s = s1 + ch; // "super!" // 字符 + string类 s = ch + s1; // "!super" cout << s << endl; // 输出"!super" return 0;}
string类对多个关系运算符进行了重载,支持字符串间的比较:
#include#include using namespace std;int main() { string s1("abcd"); string s2("abde"); // 比较s1和s2 cout << (s1 > s2) << endl; // 0 cout << (s1 < s2) << endl; // 1 cout << (s1 == s2) << endl; // 0 return 0;}
#include#include using namespace std;int main() { string s("hello string"); string::iterator it = s.begin(); while (it != s.end()) { cout << *it; it++; } cout << endl; // 输出"hello string" return 0;}
#include#include using namespace std;int main() { string s("hello string"); string::reverse_iterator rit = s.rbegin(); while (rit != s.rend()) { cout << *rit; rit++; } cout << endl; // 输出"gnirts olleh" return 0;}
#include#include using namespace std;int main() { // 方式一 string s1("hello world"); // 方式二 char str[] = "hello world"; string s2(str); cout << s1 << endl; // 输出"hello world" cout << s2 << endl; // 输出"hello world" return 0;}
#include#include using namespace std;int main() { string s("hello world "); const char* str1 = s.data(); const char* str2 = s.c_str(); cout << str1 << endl; // 输出"hello world " cout << str2 << endl; // 输出"hello world " return 0;}
#include#include using namespace std;int main() { string s1("abcdef"); string s2; // 提取pos位置开始的len个字符 s2 = s1.substr(2, 4); // "cdef" cout << s2 << endl; // 输出"cdef" return 0;}
#include#include using namespace std;int main() { string s("abcdef"); char str[20]; size_t length = s.copy(str, 4, 2); // 复制从位置2开始的4个字符 str[length] = '\0'; cout << str << endl; // 输出"dcef" return 0;}
getline函数用于读取包含空格的字符串:
#include#include using namespace std;int main() { string s; getline(cin, s); // 读取包含空格的字符串 cout << s << endl; // 输出"hello CSDN" return 0;}
#include#include using namespace std;int main() { string s; getline(cin, s, 'D'); // 使用'D'作为分隔符 cout << s << endl; // 输出"hello CS" return 0;}
以上内容系统地介绍了C++ STL中的string类,涵盖了从基础到高级操作,希望能为开发者提供全面且实用的参考。
转载地址:http://ieefk.baihongyu.com/