一、std::string
概述
std::string
是C++标准库中用于表示和操作字符串的类,属于<string>
头文件。它封装了动态字符数组,提供丰富的成员函数和操作符重载,支持高效的内存管理和字符串操作
二、构造函数(Constructors)
std::string
有多个构造函数重载,用于不同场景下的字符串初始化:
1. 默认构造函数
string str; // 创建空字符串
2. 使用C风格字符串构造
const char* cstr = "Hello";
string str1(cstr); // "Hello"
string str2("World"); // "World"
3. 使用部分字符构造
string str3("Hello World", 5); // 取前5个字符 -> "Hello"
4. 重复字符构造
string str4(5, 'a'); // "aaaaa"
5. 使用迭代器范围构造
std::vector<char> vec = {'a', 'b', 'c'};
string str5(vec.begin(), vec.end()); // "abc"
6. 拷贝构造函数
string str6(str5); // 拷贝str5的内容
三、成员函数详解
1. append()
:追加内容
功能:在字符串末尾追加内容,返回*this
。
重载形式:
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); // 迭代器范围
示例:
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()
类似,但用于替换而非追加:
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。
示例:
string s = "Hello";
s.assign(5, 'x'); // "xxxxx"
3. replace()
:替换子串
功能:替换字符串中指定位置的子串。
重载形式:
// 替换位置[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);
示例:
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
。
重载形式:
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;
示例:
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
个字符的子串。
string substr(size_t pos = 0, size_t len = npos) const;
示例:
string s = "Hello World";
string sub = s.substr(6, 5); // "World"
6. compare()
:比较字符串
功能:按字典序比较字符串,返回0
(相等)、正数(大于)或负数(小于)。
重载形式:
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;
示例:
string s1 = "apple";
string s2 = "banana";
int result = s1.compare(s2); // result < 0,因为"apple" < "banana"
7. insert()
:插入内容
功能:在指定位置插入内容。
重载形式:
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);
示例:
string s = "Hello";
s.insert(5, " World"); // "Hello World"
8. erase()
:删除字符
功能:删除指定位置的字符或子串。
string& erase(size_t pos = 0, size_t len = npos);
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
示例:
string s = "Hello World";
s.erase(5, 6); // 删除从位置5开始的6个字符 -> "Helloworld"
9. c_str()
和 data()
功能:返回指向内部字符数组的const char*
指针。
const char* c_str() const noexcept;
const char* data() const noexcept;
示例:
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+
:连接字符串(非成员函数)。
示例:
string s1 = "Hello";
string s2 = s1 + " World"; // "Hello World"
s1[0] = 'h'; // "hello"