引言
最近在学习C++,下面将从基础到进阶的顺序,列出一些 C++ 中常见的字符串定义方式及其用法,包含完整代码和详细注释,加深对代码的理解。
C 风格字符串(char*或 char[])
- 定义方式
#include <iostream>
int main() {
// 字符串字面量(不可修改)
const char* str1 = "Hello";
// 字符数组(可修改)
char str2[] = "World";
// 手动初始化字符数组(需留空间给 '\0')
char str3[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
std::cout << str1 << " " << str2 << " " << str3 << std::endl;
return 0;
}
// 输出结果:Hello World Hello
特点
结尾必须是 ‘\0’
不安全,容易越界或引发未定义行为
推荐尽量用 std::string 替代
C++ 标准字符串(std::string)
- 常见定义方式与用法
#include <iostream>
#include <string>
int main() {
// 定义字符串
std::string s1 = "Hello";
std::string s2("World");
std::string s3 = s1 + ", " + s2 + "!"; // 字符串拼接
std::cout << s3 << std::endl; // 输出:Hello, World!
// 字符访问
std::cout << "第一个字符:" << s3[0] << std::endl;
std::cout << "最后一个字符:" << s3.back() << std::endl;
// 长度与清空
std::cout << "长度:" << s3.length() << std::endl;
s3.clear();
std::cout << "是否为空:" << s3.empty() << std::endl;
return 0;
}
/*
输出结果:
Hello, World!
第一个字符:H
最后一个字符:!
长度:13
是否为空:1
*/
字符串数组或向量
- 使用 std::vectorstd::string
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> fruits = {"apple", "banana", "cherry"};
// 添加元素
fruits.push_back("date");
// 遍历
for (const std::string& fruit : fruits) {
std::cout << fruit << std::endl;
}
return 0;
}
/*
输出结果:
apple
banana
cherry
date
*/
- 使用 std::array<std::string, N>(定长数组)
#include <iostream>
#include <array>
#include <string>
int main() {
std::array<std::string, 3> days = {"Monday", "Tuesday", "Wednesday"};
for (const auto& day : days) {
std::cout << day << std::endl;
} // 使用 auto 自动识别数据类型
return 0;
}
/*
输出结果:
Monday
Tuesday
Wednesday
*/
字符串处理常用操作
- 查找和替换
#include <iostream>
#include <string>
int main() {
std::string text = "I like apples and apples are sweet.";
// 查找第一个"apple"
size_t pos = text.find("apples");
if (pos != std::string::npos) {
std::cout << "找到位置:" << pos << std::endl;
}
// 替换第一个"apples"为"oranges"
text.replace(pos, 6, "oranges");
std::cout << "替换后:" << text << std::endl;
return 0;
}
- 子串、大小写、比较
#include <iostream>
#include <string>
int main() {
std::string s = "HelloWorld";
// 提取子串
std::string sub = s.substr(0, 5); // "Hello"
// 比较
if (s == "HelloWorld") {
std::cout << "字符串相等" << std::endl;
}
// 大小写转换(手动方式)
for (char& c : s) {
c = tolower(c); // 或者 toupper(c)
}
std::cout << s << std::endl;
return 0;
}
其他容器中的字符串用法
- std::dequestd::string
#include <iostream>
#include <deque>
#include <string>
int main() {
std::deque<std::string> queue;
queue.push_back("first");
queue.push_back("second");
queue.push_front("zero");
for (const auto& item : queue) {
std::cout << item << std::endl;
}
return 0;
}
- std::map<std::string, std::string>
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, std::string> dict;
dict["apple"] = "苹果";
dict["banana"] = "香蕉";
for (const auto& [eng, chi] : dict) {
std::cout << eng << ": " << chi << std::endl;
}
return 0;
}
字符串与数值转换
#include <iostream>
#include <string>
int main() {
std::string numStr = "123";
int num = std::stoi(numStr); // string -> int
double d = std::stod("3.1415"); // string -> double
std::string s = std::to_string(42); // int -> string
std::cout << "整数:" << num << ",浮点数:" << d << ",字符串:" << s << std::endl;
return 0;
}