一、引言在 C 编程中string是处理字符串的核心类相较于 C 风格字符数组它自动管理内存、提供丰富操作接口极大提升了开发效率与代码安全性。本文将从深浅拷贝原理、string底层模拟实现、标准库string常用函数详解、迭代器与容器实战、字符串数字相加案例等维度全面梳理string类的核心知识形成从原理到实战的完整体系。二、浅拷贝与深拷贝的基本概念2.1 浅拷贝浅拷贝仅复制对象成员变量的值若成员包含指针仅拷贝指针地址使多个对象指向同一块内存。问题对象析构时同一块内存会被重复释放导致程序崩溃或悬空指针。本质位拷贝只拷贝指针不拷贝指向的数据。2.2 深拷贝深拷贝会为指针成员重新分配独立内存并复制原数据使每个对象拥有专属内存空间互不干扰。优势避免内存重复释放、数据篡改等问题是string类的默认拷贝机制。本质重新开辟内存 数据拷贝。三、C 中 string 类的默认行为C 标准库std::string的拷贝构造函数、赋值运算符均实现深拷贝修改一个对象不会影响另一个#include iostream #include string using namespace std; int main() { string original Hello, World!; string copy1(original); // 拷贝构造深拷贝 string copy2 original; // 赋值运算符深拷贝 copy1[0] h; // 修改copy1original、copy2不受影响 cout original endl; // Hello, World! cout copy1 endl; // hello, World! cout copy2 endl; // Hello, World! return 0; }四、自定义字符串类 MyString 实现深浅拷贝 核心功能4.1 基础框架构造、析构、深浅拷贝#include iostream #include cstring #include assert.h using namespace std; class MyString { private: char* _str; size_t _size; size_t _capacity; public: // 构造函数 MyString(const char* str ) { if (str nullptr) str ; _size strlen(str); _capacity _size; _str new char[_capacity 1]; strcpy(_str, str); } // 拷贝构造深拷贝 MyString(const MyString other) { _size other._size; _capacity other._capacity; _str new char[_capacity 1]; memcpy(_str, other._str, _size 1); } // 赋值运算符现代写法 MyString operator(MyString tmp) { swap(tmp); return *this; } // 移动赋值C11转移资源 MyString operator(MyString other) noexcept { if (this ! other) { delete[] _str; _str other._str; _size other._size; _capacity other._capacity; other._str nullptr; } return *this; } // 析构函数 ~MyString() { delete[] _str; _str nullptr; _size _capacity 0; } // 交换成员 void swap(MyString s) { std::swap(_str, s._str); std::swap(_size, s._size); std::swap(_capacity, s._capacity); } };4.2 比较运算符重载 !基于strcmp实现字典序比较bool operator(const MyString other) const { return strcmp(_str, other._str) 0; } bool operator(const MyString other) const { return strcmp(_str, other._str) 0; } bool operator(const MyString other) const { return !(*this other); } bool operator(const MyString other) const { return !(*this other); } bool operator(const MyString other) const { return strcmp(_str, other._str) 0; } bool operator!(const MyString other) const { return !(*this other); }4.3 resize 函数实现调整字符串长度支持缩小、扩大与填充字符void resize(size_t new_size, char fill_char \0) { if (new_size _size) { _size new_size; _str[_size] \0; } else { reserve(new_size); for (size_t i _size; i new_size; i) { _str[i] fill_char; } _size new_size; _str[_size] \0; } } // 预留容量 void reserve(size_t n) { if (n _capacity) { char* new_str new char[n 1]; strcpy(new_str, _str); delete[] _str; _str new_str; _capacity n; } }4.4 运算符重载字符串拼接支持MyString MyString、MyString const char*MyString operator(const MyString other) const { size_t new_size _size other._size; char* new_data new char[new_size 1]; strcpy(new_data, _str); strcat(new_data, other._str); MyString res(new_data); delete[] new_data; return res; } MyString operator(const char* str) const { size_t len strlen(str); size_t new_size _size len; char* new_data new char[new_size 1]; strcpy(new_data, _str); strcat(new_data, str); MyString res(new_data); delete[] new_data; return res; }4.5 迭代器实现简单模拟string迭代器本质是字符指针typedef char* iterator; typedef const char* const_iterator; iterator begin() { return _str; } iterator end() { return _str _size; } const_iterator begin() const { return _str; } const_iterator end() const { return _str _size; }五、标准库 string 常用函数详解5.1 初始化方式string s0; // 空字符串 string s1(hello); // 字面量初始化 string s2 s1; // 拷贝初始化 string s3(5, a); // 重复字符aaaaa5.2 容量与长度size()/length()返回字符串长度功能完全一致capacity()返回已分配内存容量reserve(n)预分配容量减少扩容shrink_to_fit()释放多余内存使容量等于长度clear()清空字符串内容不释放内存5.3 字符串操作拼接、append()查找find()、rfind()、find_first_of()未找到返回string::npos子串substr(pos, len)省略len取到末尾替换replace(pos, len, str)插入 / 删除insert()、erase()效率低少用字符访问[]不检查越界、at()越界抛异常5.4 数值与字符串转换数值→字符串to_string()C11标准通用字符串→整数atoi()C 风格、stoi()C11抛异常通用转换stringstream跨平台、支持任意类型5.5 输入输出cin以空格 / 换行分隔无法读取带空格字符串getline(cin, str)读取整行包含空格需处理前导换行符cin.ignore(); // 清空缓冲区换行 getline(cin, str);六、迭代器与容器配合6.1 string 遍历方式下标遍历for (size_t i 0; i s.size(); i) cout s[i];迭代器遍历string::iterator it s.begin(); while (it ! s.end()) cout *it;反向迭代器string::reverse_iterator rit s.rbegin(); while (rit ! s.rend()) cout *rit;范围 for 循环简洁推荐for (auto ch : s) cout ch;6.2 STL 算法配合需包含algorithmreverse(s.begin(), s.end()); // 反转 sort(s.begin(), s.end()); // 排序七、实战案例字符串数字相加处理超大数字超出整型范围模拟手工加法#include iostream #include string #include algorithm using namespace std; string addStrings(string num1, string num2) { int i num1.size() - 1, j num2.size() - 1; string res; int carry 0; while (i 0 || j 0 || carry) { int val1 i 0 ? num1[i--] - 0 : 0; int val2 j 0 ? num2[j--] - 0 : 0; int sum val1 val2 carry; carry sum / 10; res.push_back(sum % 10 0); } reverse(res.begin(), res.end()); return res; } // 测试 int main() { cout addStrings(123456789, 987654321) endl; // 1111111110 return 0; }八、总结深浅拷贝浅拷贝共享内存易崩溃深拷贝独立内存更安全string默认深拷贝。自定义string需实现构造、析构、拷贝构造、赋值运算符重载常用运算符与迭代器。标准string接口丰富优先使用、reserve优化性能注意find、substr边界。迭代器是 STL 容器通用访问方式配合算法可高效操作字符串。实战字符串数字相加核心为逆序逐位相加 处理进位 反转结果适用于超大数运算。本文覆盖string原理、实现、使用、实战全流程是 C 字符串编程的核心参考资料。