std::string 常用函数及返回值详解

1. 构造与初始化

构造函数无显式“返回值”(本质是创建 std::string 对象),此处补充各构造方式的 对象状态说明

初始化方式 返回/对象状态(创建的 string 对象) 关键说明
string s1; 空字符串(size() == 0empty() == true 默认构造,内部无有效字符
string s2("hello"); 内容为 "hello" 的字符串(size() == 5 从 C 风格字符串(const char*)拷贝构造
string s3(5, 'a'); 内容为 "aaaaa" 的字符串(size() == 5 n 个相同字符构造,n 需非负
string s4(s2); s2 内容完全相同的字符串 拷贝构造,新对象与原对象独立(深拷贝)
string s5 = "world"; 内容为 "world" 的字符串(size() == 5 赋值初始化,等价于 string s5("world")
string s6(s2, 1, 3); s2[1] 开始,长度为 3 的子串("ell" pos 超出 s2.size(),抛出 out_of_range 异常;若 n 超出剩余字符,取到字符串末尾

2. 基本属性与访问

函数 函数原型 返回值类型 功能与返回值说明
size() size_t size() const; size_t 返回字符串的 有效字符数(不含末尾隐藏的 \0),与 length() 功能完全一致
length() size_t length() const; size(),历史接口(早期为匹配 C 风格字符串 strlen() 命名)
empty() bool empty() const; bool 判断字符串是否为空:
- truesize() == 0(空字符串)
- falsesize() > 0(非空)
[] char& operator[](size_t pos); char& 访问索引 pos 处的字符(无越界检查):
- 返回该字符的引用,可修改(如 s[0] = 'H'
- 若 pos >= size(),行为未定义(可能崩溃)
const char& operator[](size_t pos) const; const char& 重载版本,用于 const 对象,返回的字符不可修改
at() char& at(size_t pos); char& 访问索引 pos 处的字符(有越界检查):
- 若 pos >= size(),抛出 out_of_range 异常
- 返回字符引用,可修改(非 const 版本)
const char& at(size_t pos) const; const char& 重载版本,用于 const 对象,返回的字符不可修改
c_str() const char* c_str() const; const char* 返回指向字符串内部的 C 风格字符串指针(末尾含 \0):
- 指针有效期与原 string 对象绑定(原对象修改/销毁后,指针失效)
- 不可修改返回的指针指向的内容(const 修饰)
data() const char* data() const; C++11 后与 c_str() 功能一致(返回含 \0 的 C 风格指针);C++11 前不含 \0,需注意版本差异

3. 字符串修改

修改类函数通常返回 *this(当前 std::string 对象的引用),支持链式调用(如 s.append("a").append("b")),部分函数有特殊返回值:

函数 函数原型(关键版本) 返回值类型 功能与返回值说明
assign() string& assign(const char* s); string& 用新内容(如 C 风格字符串 s)覆盖原有内容,返回 *this(修改后的当前对象)
string& assign(size_t n, char c); n 个字符 c 覆盖原有内容,返回 *this
append() string& append(const char* s); 在字符串末尾追加内容(如 s),返回 *this;若追加空指针,行为未定义
string& append(size_t n, char c); 在末尾追加 n 个字符 c,返回 *this
push_back() void push_back(char c); void 在字符串末尾追加 单个字符 c,无返回值;效率比 append(1, c) 更高
insert() string& insert(size_t pos, const char* s); string& 在索引 pos 处插入内容 s,返回 *this;若 pos > size(),抛出 out_of_range 异常
erase() string& erase(size_t pos = 0, size_t n = npos); pos 开始删除 n 个字符(n 默认为 string::npos,即删除到末尾),返回 *this;若 pos > size(),抛出异常
clear() void clear(); void 清空字符串(size() 变为 0,empty() 变为 true),无返回值;不释放内存(仅标记无效)
replace() string& replace(size_t pos, size_t n, const char* s); string& pos 开始,删除 n 个字符,再插入 s,返回 *this;若 pos > size(),抛出异常

4. 字符串查找

所有查找函数返回 size_t 类型,表示“找到的位置索引”;若未找到,返回特殊值 string::npos(需包含 <string> 头文件才能使用):

函数 函数原型(关键版本) 返回值类型 功能与返回值说明
find() size_t find(const char* s, size_t pos = 0) const; size_t pos 开始(默认从 0 开始),查找 s 首次出现的位置:
- 找到:返回 s 第一个字符在原字符串中的索引
- 未找到:返回 string::npos,在大部分情况中,因为 size_type 相当于 unsigned int类型,最大值4294967295强制转换为int型,就是-1
rfind() size_t rfind(const char* s) const; 从字符串末尾开始,查找 s 最后一次出现的位置:
- 找到:返回 s 第一个字符的索引
- 未找到:返回 string::npos
find_first_of() size_t find_first_of(const char* s) const; 查找 s任一字符 在原字符串中首次出现的位置:
- 例:"hello".find_first_of("aeiou") 返回 1('e' 的索引)
- 未找到:返回 string::npos
find_last_not_of() size_t find_last_not_of(const char* s) const; 查找原字符串中 不在 s 中的最后一个字符 的位置:
- 例:"hello".find_last_not_of("lo") 返回 1('e' 的索引)
- 若所有字符都在 s 中,返回 string::npos

5. 字符串比较

比较函数返回 表示“大小关系”的值,关系运算符直接返回布尔值,compare() 返回整数:

比较方式 函数原型/运算符 返回值类型 功能与返回值说明
== bool operator==(const string& rhs) const; bool 判断两个字符串是否完全相同(字符序列、长度均一致):
- true:相同;false:不同
!= bool operator!=(const string& rhs) const; == 相反:true 表示不同,false 表示相同
< / > / <= / >= 类似上述原型 字典序 比较(逐字符比较 ASCII 码):
- 例:"apple" < "banana" 返回 true('a' < 'b')
- 短字符串是长字符串的前缀时,短字符串更小(如 "app" < "apple"
compare() int compare(const string& rhs) const; int 比较当前字符串与 rhs
- 返回 0:两者相等
- 返回 正数:当前字符串更大
- 返回 负数:当前字符串更小
int compare(size_t pos, size_t n, const string& rhs) const; 用当前字符串从 pos 开始的 n 个字符,与 rhs 比较,返回值规则同上

6. 子串与转换

函数 函数原型 返回值类型 功能与返回值说明
substr() string substr(size_t pos = 0, size_t n = npos) const; string 提取子串并返回新的 string 对象:
- 从 pos 开始,提取 n 个字符;若 nnpos 或超出剩余字符,取到字符串末尾
- 若 pos >= size(),抛出 out_of_range 异常
stoi() int stoi(const string& s, size_t* idx = nullptr, int base = 10); int 将字符串 s 转换为 int 类型整数
- idx(可选):存储第一个未转换字符的索引
- base(可选):进制(默认 10,支持 2-36)
- 若无法转换(如 s 非数字),抛出 invalid_argument 异常;若超出 int 范围,抛出 out_of_range 异常
stol() long stol(...) long stoi(),但返回 long 类型(支持更大范围)
stod() double stod(...) double 将字符串 s 转换为 double 类型浮点数,异常规则同 stoi()
to_string() string to_string(int val); string 将数值(如 intlongdouble 等)转换为对应的字符串:
- 例:to_string(123) 返回 "123"to_string(3.14) 返回 "3.140000"

7. 其他常用操作

函数/操作 函数原型 返回值类型 功能与返回值说明
swap()(成员函数) void swap(string& rhs); void 交换当前字符串与 rhs 的内容(高效,仅交换内部指针,无数据拷贝),无返回值
swap()(全局函数) void swap(string& lhs, string& rhs); 同成员函数 swap(),全局版本,更通用(如交换两个 string 对象)
trim()(自定义) string trim(const string& s); string 自定义函数,返回去除首尾空格后的新字符串(原字符串不变);空格包括 ' '\t\n 等(isspace() 判定)

关键补充说明

  1. size_t 类型
    是无符号整数类型(通常与 unsigned intunsigned long 等价),因此不是什么都可以用 -1 判断“未找到”(string::npos 本质是 size_t 的最大值,与 -1 比较可能会类型转换导致逻辑错误)。

  2. 异常安全
    涉及“索引访问”“子串提取”的函数(如 at()substr()insert()),若参数越界会抛出 out_of_range 异常;数值转换函数(如 stoi())会因“无法转换”“范围溢出”抛出 invalid_argumentout_of_range 异常,使用时可按需捕获。

  3. 返回引用的函数
    append()erase()replace() 等返回 *this,支持链式调用(如 s.erase(2,3).append("xyz")),但需注意:若链式调用中某一步抛出异常,后续操作不会执行。

0 条评论

目前还没有评论...