为什么选择使用 string
来表示字符串?char
不足够吗?
在 C++ 中,字符的数据类型是
char
,但是使用char
只能表示单个字符。若要表示一个字符串,就需要使用字符数组
char[]
。可是,在使用
char[]
时有一个弊端:如果需要输出字符串时,需要循环遍历字符数组中的每一个元素。
这种时候,使用
string
类会更方便处理字符串。
string 字符串类的特点
没有固定的长度,不需要提前确认存储的字符串长度
直接将字符串存为一个变量,而不是多个字符
回到顶部
C++ 中,使用 string
字符串类需要包含头文件 #include<string>
。
由于 string
属于命名空间 std
的范畴,因此,也需要声明使用的命名空间。
// (A) 头文件 #include <string> // (B) 命名空间 // 方法1:直接声明使用整个命名空间 using namespace std; string s1; // 方法2:不直接声明整个命名空间,在使用 string 时需要用: std::string s1;
回到顶部
// (A) 定义一个默认的空字符串 string s1; // (B) 定义 + 用字符串初始化 string s2("happy"); string s2 = "happy"; // (C) 用已有的字符串构造新的字符串 string s3(s2); string s3(s2, 1, 3); // 以s2的下标为1的字符为起点,取3个字符,s3 = "app" string s3(s2.begin(), s2.begin() + 4); // s3 = "happ" string s3("happy life", 8); // 从字符串中取前8个字符,s3 = "happy li" // (D) 用单个字符进行构造 string s4(5,'h'); // s4 = "hhhhh"
回到顶部
string name; // 方法1:读取字符串,遇到空格时停止 cin >> name; // 方法2:读取完整的一行 getline(cin, name); // 方法3:读取,直到指定的分隔符才停止 getline(cin, name, '@'); /* 如果输入数据为: Karry Wang Zhuang WenJie @ 使用以上三种方法,得到的 name 为: 方法1 Karry 方法2 Karry Wang 方法3 Karry Wang\nZhuang WenJie\n */
回到顶部
// 使用下标访问 cout << str[1]; // 使用范围遍历 for(char c: str) { cout << c << " "; } // 清空元素 str.clear(); // 检查是否为空 if(str.empty()) { ...... } // 查询字符串长度 cout << str.size(); cout << str.length(); // 比较字符串大小(字典序考前的较小) string s1 = "abc", s2 = "bde"; if(s1 < s2) { ... } if(s1 == s2) { ... } if(s1 > s2) { ... } // 转换为 C 风格字符串 string word = "happy"; word.c_str();
回到顶部
// 添加单个字符 str.pushback('a'); // 添加字符串 str.append(s2); str += s2; // 拼接不同的字符或字符串 // 可以拼接多种数据类型,如:string、char、char*、char[] string s1 = "live"; string s2 = "happy"; char s3 = 'a'; char* s4 = "life"; char s5[] = "together"; string str = s1 + s2; string str = s1 + s3; string str = s1 + s4; string str = s1 + s5;
回到顶部
// int 转换成 string string str = to_string(1); string str = to_string(2 + 5); // ------------------------------------------------- // double 转换成 string string str1 = to_string(3.14); string str2 = to_string(3.14159265); /* 注意:str1 = "3.140000", str2 = "3.141593", 太短会自动补零,太长也会造成精度损失 */
回到顶部
// string 转换成 int int a = stoi("21"); int b = stoi("3.14"); // 转换结果:a = 21, b = 3 // 将 string 中的整数转换成 int,同时返回转换的长度 string::size_type sz; int c = stoi("21 girls", &sz); // 转换结果:c = 21, sz = 2 // 将16进制的 string 转换成10进制的 int int d = stoi("0x40c3", nullptr, 16); // 转换结果:d = 16579 // 自动检查进制,转换成10进制的 int int e = stoi("0x40c3", nullptr, 0); // 转换结果:3 = 16579 // ------------------------------------------------- /* 除了转换成 int 之外,也可以转换成 float 或 double 使用 stof 转换成 float 使用 stod 转换成 double */
回到顶部
// 在指定下标的位置插入字符串 string str = "0123456789"; string add = "happy"; str.insert(5, add); // 插入结果:str = "01234happy56789" // ------------------------------------------------- string s1, s2; s1 = s2 = "0123456789"; // 删除指定下标位置开始的所有字符 s1.erase(5); // 从指定下标位置开始,删除指定数量的元素 s2.erase(5,3); // 删除结果:s1 = "01234", s2 = "0123489"
回到顶部
string s1 = "live a happy life"; string s2; // 从指定下标位置开始提取指定数量的元素 s2 = s1.substr(7, 10);
回到顶部
// 查找并返回子字符串第一次出现的下标位置 string s1 = "live a happy happy life"; string s2 = "happy"; int ind = s1.find(s2); if(ind < s1.length()) { cout << "Found at index " << ind; } // 输出结果:Found at index 7
回到顶部