string(c++)

一.字符串方法

1. 构造函数

  • string()
    创建一个空字符串。
  • string(const string& str)
    复制构造函数,复制另一个字符串。
  • string(string&& str)
    移动构造函数,移动一个临时字符串。
  • string(const char\* s)
    从 C 风格字符串创建一个字符串。
  • string(size_t n, char c)
    创建一个包含 n 个字符 c 的字符串。

2. 赋值操作符

  • string& operator=(const string& str)
    将另一个字符串的内容赋值给当前字符串。
  • string& operator=(string&& str)
    将另一个字符串的内容移动到当前字符串。
  • string& operator=(const char\* s)
    将 C 风格字符串 s 赋值给当前字符串。

3. 访问字符

  • char& operator[](size_t pos)
    通过下标访问字符(没有范围检查)。
  • const char& operator[](size_t pos) const
    通过下标访问字符(常量版本,不可修改)。
  • char& at(size_t pos)
    通过下标访问字符,并进行范围检查。
  • const char& at(size_t pos) const
    通过下标访问字符(常量版本,带范围检查)。

4. 修改字符串

  • void clear()
    清空字符串内容,释放内存。
  • void resize(size_t n)
    调整字符串的大小为 n,多余部分用空字符填充。
  • void resize(size_t n, char c)
    调整字符串大小为 n,新增加的字符用 c 填充。
  • void append(const string& str)
    将字符串 str 追加到当前字符串。
  • void append(const char\* s)
    将 C 风格字符串 s 追加到当前字符串。
  • void append(const char\* s, size_t n)
    将 C 风格字符串 s 中的前 n 个字符追加到当前字符串。
  • void push_back(char c)
    将字符 c 追加到字符串末尾。
  • string& operator+=(const string& str)
    将字符串 str 追加到当前字符串。
  • string& operator+=(const char\* s)
    将 C 风格字符串 s 追加到当前字符串。

5. 查找字符串

  • size_t find(const string& str, size_t pos = 0) const
    查找字符串 str 在当前字符串中的首次出现位置。
  • size_t find(const char\* s, size_t pos = 0) const
    查找 C 风格字符串 s 在当前字符串中的首次出现位置。
  • size_t find(char c, size_t pos = 0) const
    查找字符 c 在当前字符串中的首次出现位置。
  • size_t rfind(const string& str, size_t pos = string::npos) const
    从字符串的末尾开始查找 str
  • size_t rfind(const char\* s, size_t pos = string::npos) const
    从字符串的末尾开始查找 C 风格字符串 s
  • size_t rfind(char c, size_t pos = string::npos) const
    从字符串的末尾开始查找字符 c

6. 子字符串操作

  • string substr(size_t pos = 0, size_t len = string::npos) const
    返回从 pos 位置开始,长度为 len 的子字符串。
  • string& replace(size_t pos, size_t len, const string& str)
    pos 位置开始,替换 len 长度的内容为字符串 str
  • string& replace(size_t pos, size_t len, const char\* s)
    pos 位置开始,替换 len 长度的内容为 C 风格字符串 s

7. 比较字符串

  • int compare(const string& str) const
    比较当前字符串与 str,返回值:
    • 小于 0:当前字符串小于 str
    • 0:当前字符串等于 str
    • 大于 0:当前字符串大于 str
  • int compare(size_t pos, size_t len, const string& str) const
    比较当前字符串从 pos 开始,长度为 len 的部分与 str

8. 获取字符串信息

  • size_t size() const
    返回字符串的长度(字符数)。
  • size_t length() const
    返回字符串的长度,等同于 size()
  • size_t capacity() const
    返回字符串当前分配的内存大小(以字符数为单位)。
  • bool empty() const
    判断字符串是否为空。
  • const char\* c_str() const
    返回一个 C 风格字符串(以 null 结尾的字符数组)。

9. 其他方法

  • string& erase(size_t pos = 0, size_t len = string::npos)
    删除从 pos 位置开始,长度为 len 的部分。
  • string& insert(size_t pos, const string& str)
    pos 位置插入字符串 str
  • string& insert(size_t pos, const char\* s)
    pos 位置插入 C 风格字符串 s
  • string& insert(size_t pos, size_t len, char c)
    pos 位置插入 len 个字符 c

10. 转换操作

  • string& tolower()
    将字符串中的所有字母转换为小写(需手动实现,C++ 标准库没有内建方法)。
  • string& toupper()
    将字符串中的所有字母转换为大写(需手动实现,C++ 标准库没有内建方法)。

二.程序举列

1. 构造函数与赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

int main() {
// 默认构造
string str1;
cout << "str1: '" << str1 << "'" << endl; // 输出空字符串

// 从另一个字符串复制
string str2 = "Hello, world!";
cout << "str2: " << str2 << endl;

// 从C风格字符串构造
const char* cstr = "C style string";
string str3(cstr);
cout << "str3: " << str3 << endl;

// 移动构造
string str4 = move(str2);
cout << "str4 (after move): " << str4 << endl;
cout << "str2 (after move): " << str2 << endl; // str2 是空字符串
}

2. 字符串修改与追加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

int main() {
string str = "Hello";

// 追加另一个字符串
str.append(", world!");
cout << "After append: " << str << endl;

// 追加字符
str.push_back(' ');
str.push_back('G');
cout << "After push_back: " << str << endl;

// 使用 operator+= 追加字符串
str += " How are you?";
cout << "After operator+=: " << str << endl;
}

3. 字符访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

int main() {
string str = "Hello, world!";

// 使用下标访问字符
cout << "First character: " << str[0] << endl; // 输出 'H'

// 使用 at() 访问字符(带范围检查)
cout << "Character at index 7: " << str.at(7) << endl; // 输出 'w'

// 注意: 使用 at() 如果越界会抛出异常
try {
cout << str.at(20) << endl; // 会抛出异常
} catch (const out_of_range& e) {
cout << "Exception: " << e.what() << endl;
}
}

4. 查找字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

int main() {
string str = "Hello, world!";

// 查找子字符串的位置
size_t pos = str.find("world");
if (pos != string::npos) {
cout << "'world' found at index: " << pos << endl; // 输出 7
}

// 查找字符的位置
pos = str.find('o');
cout << "First 'o' found at index: " << pos << endl; // 输出 4

// 查找一个不存在的子字符串
pos = str.find("notfound");
if (pos == string::npos) {
cout << "'notfound' not found!" << endl; // 输出未找到
}
}

5. 子字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

int main() {
string str = "Hello, world!";

// 提取子字符串
string substr = str.substr(7, 5);
cout << "Substring: " << substr << endl; // 输出 'world'

// 提取从索引7到结束的子字符串
string substr2 = str.substr(7);
cout << "Substring from index 7: " << substr2 << endl; // 输出 'world!'
}

6. 字符串替换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

int main() {
string str = "Hello, world!";

// 替换部分字符串
str.replace(7, 5, "C++");
cout << "After replace: " << str << endl; // 输出 'Hello, C++!'

// 替换为多个字符
str.replace(7, 3, "Programming");
cout << "After replace with longer string: " << str << endl; // 输出 'Hello, Programming!'
}

7. 字符串比较

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

int main() {
string str1 = "apple";
string str2 = "banana";
string str3 = "apple";

// 字符串比较
if (str1 == str2) {
cout << "str1 equals str2" << endl;
} else {
cout << "str1 does not equal str2" << endl; // 输出
}

if (str1 == str3) {
cout << "str1 equals str3" << endl; // 输出
}
}

8. 字符串清空与检查是否为空

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main() {
string str = "Hello, world!";

// 清空字符串
str.clear();
cout << "After clear: '" << str << "'" << endl; // 输出空字符串

// 检查是否为空
if (str.empty()) {
cout << "String is empty" << endl; // 输出
}
}

9. 字符串长度与容量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

int main() {
string str = "Hello";

// 获取字符串的长度
cout << "Length: " << str.length() << endl; // 输出 5

// 获取字符串的容量
cout << "Capacity: " << str.capacity() << endl; // 输出 > 5

// 增加字符并查看容量
str += " World!";
cout << "New length: " << str.length() << endl; // 输出 12
cout << "New capacity: " << str.capacity() << endl; // 输出变化
}