创建String对象
可以使用以下方式来创建String对象:
// 从字符串字面量创建
std::string str1 = "Hello World";
// 从字符数组创建
char arr[] = {'H', 'e', 'l', 'l', 'o', '\0'};
std::string str2(arr);
// 从另一个String对象创建
std::string str3(str1);
// 从部分字符创建
std::string str4(str1, 6); // 创建 "World"
注意,在使用字符数组创建String对象时,需要保证数组以null字符结尾。
建议:在创建String对象时,尽量使用字面量的方式,这样代码更加简洁明了。
访问String对象
可以使用下标运算符和迭代器来访问String对象中的字符,如下所示:
std::string str = "Hello World";
char c1 = str[0]; // 获取第一个字符 'H'
char c2 = str.at(1); // 获取第二个字符 'e'
// 使用迭代器遍历字符串
for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
std::cout << *it;
}
注意,使用下标运算符访问String对象中的字符时,需要确保索引不越界。如果越界,程序将会出现未定义行为。
建议:使用迭代器遍历字符串可以避免越界的问题,并且可以支持反向遍历和修改字符串。
添加和删除字符
std::string str = "Hello";
str.push_back(' '); // 添加一个空格字符
str.push_back('W'); // 添加一个字符 'W'
str.pop_back(); // 删除最后一个字符
还可以使用insert()和erase()函数来插入和删除字符串中的任意位置的字符,如下所示:
std::string str = "Hello World";
str.insert(5, "C++"); // 插入字符串 "C++",结果为 "Hello C++World"
str.erase(5, 3); // 删除从索引为 5 开始的 3 个字符,结果为 "HelloWorld"
建议:在添加或删除字符时,需要注意字符串的长度是否足够,以及索引是否越界。
查找和替换字符
可以使用find()和rfind()函数来查找字符串中的子串,并返回子串的起始位置,如下所示:
std::string str = "Hello World";
int pos = str.find("World"); // 查找子串 "World",返回起始位置 6
int rpos = str.rfind("l"); // 从后往前查找字符 'l',返回最后一个 '
可以使用replace()函数来替换字符串中的子串,如下所示
std::string str = "Hello World";
str.replace(6, 5, "C++"); // 替换从索引 6 开始的 5 个字符为字符串 "C++",结果为 "Hello C++"
建议:在查找和替换字符时,需要注意字符串中是否包含要查找或替换的子串。
转换为其他类型
可以使用to_string()函数将数字类型转换为String类型,如下所示:
int num = 123;
std::string str = std::to_string(num); // 将整数转换为字符串 "123"
可以使用stod()、stof()、stoi()等函数将字符串转换为其他类型,如下所示:
std::string str1 = "3.14159";
double num1 = std::stod(str1); // 将字符串转换为 double 类型的 3.14159
std::string str2 = "123";
int num2 = std::stoi(str2); // 将字符串转换为 int 类型的 123
建议:在转换类型时,需要确保字符串的格式正确,否则会出现转换失败的情况。
比较String对象
可以使用比较运算符(==、!=、<、>、<=、>=)来比较两个String对象的大小,如下所示:
std::string str1 = "Hello";
std::string str2 = "World";
if (str1 == str2) {
std::cout << "str1 equals to str2" << std::endl;
} else if (str1 < str2) {
std::cout << "str1 is less than str2" << std::endl;
} else {
std::cout << "str1 is greater than str2" << std::endl;
}
建议:在比较字符串时,需要注意字符串的长度和字符顺序。
其他常用函数
除了上述函数之外,还有一些常用的String函数,如下所示:
std::string str = " Hello ";
str.length(); // 返回字符串的长度,不包括结尾的 null 字符
str.size(); // 与 length() 函数功能相同
str.capacity(); // 返回字符串当前的容量,即分配的内存大小
str.shrink_to_fit(); // 释放不需要的内存,缩小字符串的容量
str.clear(); // 清空字符串中的所有字符
建议:在使用String对象时,可以使用capacity()函数来了解字符串当前的容量,如果容量过大可以使用shrink_to_fit()函数来释放不需要的内存。
下面是完整的示例代码:
#include <iostream>
#include <string>
int main() {
// 创建String对象
std::string str1 = "Hello World";
char arr[] = {'H', 'e', 'l', 'l', 'o', '\0'};
std::string str2(arr);
std::string str3(str1);
std::string str4(str1, 6);
// 访问
// 获取子串
std::string sub_str = str1.substr(6, 5);
str1.insert(11, "!");
// 删除字符
str1.erase(5, 6);
// 替换字符
str1.replace(6, 5, "C++");
// 转换为其他类型
int num = 123;
std::string num_str = std::to_string(num);
std::string str = "3.14159";
double num1 = std::stod(str);
std::string str2 = "123";
int num2 = std::stoi(str2);
// 比较String对象
std::string str5 = "Hello";
std::string str6 = "World";
if (str5 == str6) {
std::cout << "str5 equals to str6" << std::endl;
} else if (str5 < str6) {
std::cout << "str5 is less than str6" << std::endl;
} else {
std::cout << "str5 is greater than str6" << std::endl;
}
// 其他常用函数
std::string str7 = " Hello ";
std::cout << "str7 length: " << str7.length() << std::endl;
std::cout << "str7 capacity: " << str7.capacity() << std::endl;
str7.shrink_to_fit();
std::cout << "str7 capacity after shrink: " << str7.capacity() << std::endl;
str7.clear();
std::cout << "str7 length after clear: " << str7.length() << std::endl;
return 0;
}
在使用String对象时,需要注意以下几点:
String对象是可变的,可以通过修改对象中的字符来改变对象本身,但需要注意对象中字符的长度。
在使用String对象时,需要注意对象的长度和容量,可以使用length()和capacity()函数来了解对象的长度和容量,使用shrink_to_fit()函数来释放不需要的内存。
在查找和替换字符时,需要注意字符串中是否包含要查找或替换的子串。
在转换类型时,需要确保字符串的格式正确,否则会出现转换失败的情况。
在比较字符串时,需要注意字符串的长度和字符顺序。总之,String是一个非常常用的C++STL库中的类,掌握了它的使用方法,可以方便地操作字符串。