C++ string

一、std::string 概述

std::string 是C++标准库中用于表示和操作字符串的类,属于<string>头文件。它封装了动态字符数组,提供丰富的成员函数和操作符重载,支持高效的内存管理和字符串操作

二、构造函数(Constructors)

std::string 有多个构造函数重载,用于不同场景下的字符串初始化:

1. 默认构造函数

cpp 复制代码
string str;  // 创建空字符串

2. 使用C风格字符串构造

cpp 复制代码
const char* cstr = "Hello";
string str1(cstr);    // "Hello"
string str2("World"); // "World"

3. 使用部分字符构造

cpp 复制代码
string str3("Hello World", 5); // 取前5个字符 -> "Hello"

4. 重复字符构造

cpp 复制代码
string str4(5, 'a'); // "aaaaa"

5. 使用迭代器范围构造

cpp 复制代码
std::vector<char> vec = {'a', 'b', 'c'};
string str5(vec.begin(), vec.end()); // "abc"

6. 拷贝构造函数

cpp 复制代码
string str6(str5); // 拷贝str5的内容

三、成员函数详解

1. append():追加内容

功能 :在字符串末尾追加内容,返回*this
重载形式

cpp 复制代码
string& append(const string& str);
string& append(const char* s);
string& append(const char* s, size_t n);  // 追加s的前n个字符
string& append(size_t n, char c);         // 追加n个字符c
string& append(InputIt first, InputIt last); // 迭代器范围

示例

cpp 复制代码
string s = "Hello";
s.append(" World");       // "Hello World"
s.append(3, '!');         // "Hello World!!!"
s.append(s.begin(), s.begin()+5); // 追加自身前5个字符 -> "Hello World!!!Hello"

2. assign():替换内容

功能 :替换当前字符串内容,返回*this
重载形式append()类似,但用于替换而非追加:

cpp 复制代码
string& assign(const string& str);
string& assign(const char* s);
string& assign(const char* s, size_t n);//从指针s指向的位置复制前n个字符替换
string& assign(size_t n, char c);//将字符串替换为n个重复的字符c。

示例

cpp 复制代码
string s = "Hello";
s.assign(5, 'x'); // "xxxxx"

3. replace():替换子串

功能 :替换字符串中指定位置的子串。
重载形式

cpp 复制代码
// 替换位置[pos, pos+len)的内容
string& replace(size_t pos, size_t len, const string& str);
string& replace(size_t pos, size_t len, const char* s);
string& replace(size_t pos, size_t len, const char* s, size_t n); // 使用s的前n个字符
string& replace(size_t pos, size_t len, size_t n, char c);

// 使用迭代器范围替换
string& replace(iterator first, iterator last, const string& str);

示例

cpp 复制代码
string s = "Hello World";
s.replace(6, 5, "Universe");   // "Hello Universe"
s.replace(s.begin(), s.begin()+5, "Hi"); // "Hi Universe"

4. find():查找子串

功能 :返回子串首次出现的位置,未找到则返回string::npos
重载形式

cpp 复制代码
size_t find(const string& str, size_t pos = 0) const;
size_t find(const char* s, size_t pos = 0) const;
size_t find(const char* s, size_t pos, size_t n) const; // 查找s的前n个字符
size_t find(char c, size_t pos = 0) const;

示例

cpp 复制代码
string s = "Hello World";
size_t pos = s.find("World"); // pos = 6
if (pos != string::npos) {
    cout << "Found at position " << pos << endl;
}

5. substr():获取子串

功能 :返回从pos开始的len个字符的子串。

cpp 复制代码
string substr(size_t pos = 0, size_t len = npos) const;

示例

cpp 复制代码
string s = "Hello World";
string sub = s.substr(6, 5); // "World"

6. compare():比较字符串

功能 :按字典序比较字符串,返回0(相等)、正数(大于)或负数(小于)。
重载形式

cpp 复制代码
int compare(const string& str) const;
int compare(size_t pos, size_t len, const string& str) const;
int compare(const char* s) const;
int compare(size_t pos, size_t len, const char* s, size_t n) const;

示例

cpp 复制代码
string s1 = "apple";
string s2 = "banana";
int result = s1.compare(s2); // result < 0,因为"apple" < "banana"

7. insert():插入内容

功能 :在指定位置插入内容。
重载形式

cpp 复制代码
string& insert(size_t pos, const string& str);
string& insert(size_t pos, const char* s);
string& insert(size_t pos, const char* s, size_t n);
string& insert(size_t pos, size_t n, char c);

示例

cpp 复制代码
string s = "Hello";
s.insert(5, " World"); // "Hello World"

8. erase():删除字符

功能:删除指定位置的字符或子串。

cpp 复制代码
string& erase(size_t pos = 0, size_t len = npos);
iterator erase(iterator position);
iterator erase(iterator first, iterator last);

示例

cpp 复制代码
string s = "Hello World";
s.erase(5, 6); // 删除从位置5开始的6个字符 -> "Helloworld"

9. c_str()data()

功能 :返回指向内部字符数组的const char*指针。

cpp 复制代码
const char* c_str() const noexcept;
const char* data() const noexcept;

示例

cpp 复制代码
string s = "Hello";
const char* p = s.c_str(); // 指向以'\0'结尾的C字符串

10. 容量相关函数

  • size()/ length():返回字符数量。
  • capacity():返回当前分配的内存容量。
  • reserve(size_t n):预分配至少n个字符的内存。
  • shrink_to_fit():减少内存占用以适应实际大小。

四、操作符重载

  • operator=:赋值操作。
  • operator+=:追加内容。
  • operator[]:访问字符(不检查越界)。
  • operator+:连接字符串(非成员函数)。

示例

cpp 复制代码
string s1 = "Hello";
string s2 = s1 + " World"; // "Hello World"
s1[0] = 'h'; // "hello"
相关推荐
Biomamba生信基地30 分钟前
R语言基础| 下载、安装
开发语言·r语言·生信·医药
姜君竹31 分钟前
QT的工程文件.pro文件
开发语言·c++·qt·系统架构
思捻如枫34 分钟前
C++数据结构和算法代码模板总结——算法部分
数据结构·c++
奇树谦36 分钟前
使用VTK还是OpenGL集成到qt程序里哪个好?
开发语言·qt
VBA63371 小时前
VBA之Word应用第三章第十节:文档Document对象的方法(三)
开发语言
老胖闲聊1 小时前
Python Rio 【图像处理】库简介
开发语言·图像处理·python
码界奇点1 小时前
Python Flask文件处理与异常处理实战指南
开发语言·python·自然语言处理·flask·python3.11
贩卖纯净水.1 小时前
浏览器兼容-polyfill-本地服务-优化
开发语言·前端·javascript
weixin_478689761 小时前
C++ 对 C 的兼容性
java·c语言·c++
k要开心2 小时前
C++概念以及基础框架语法
开发语言·c++