- lizexuan's blog
浅谈 string
- 2024-4-12 18:03:08 @
介绍
在 C++ 中,string
是标准库中的一种数据类型,用于表示字符串。它是 C++ 中对字符串操作的一种更加方便和高效的方式,相比于使用字符数组( char
数组)来存储字符串,string 类提供了更多的功能和操作方法。
使用 string
类型可以更加轻松地进行字符串的操作,包括字符串的连接、查找、替换、子串提取等。此外,string
类还提供了许多成员函数和重载运算符,使得字符串的操作更加简洁和易于理解。
例如,可以使用 +
运算符来连接两个字符串
#include<bits/stdc++.h>
using namespace std;
int main() {
string str1 = "Hello";
string str2 = "World";
string result = str1 + " " + str2; // 连接两个字符串
cout << result << endl; // 输出结果:Hello World
return 0;
}
另外,string
类还提供了丰富的构造函数和成员函数,可以方便地进行字符串的初始化和操作。例如,可以使用 size()
函数获取字符串的长度,使用 substr()
函数获取子串等。
string中的重载运算符
string
类在 C++ 中重载了一系列运算符,以便更方便地进行字符串操作。下面是 string
类重载的一些常用运算符及其功能:
std::string
类在 C++ 中重载了一系列运算符,以便更方便地进行字符串操作。下面是 std::string
类重载的一些常用运算符及其功能:
1. +
运算符:用于连接两个字符串,将两个字符串合并为一个字符串。
string str1 = "Hello";
string str2 = "World";
string result = str1 + str2; // 将 str1 和 str2 连接起来
2. +=
运算符:用于将一个字符串连接到另一个字符串的末尾。
string str = "Hello";
str += " World"; // 在 str 的末尾添加 " World"
3. ==
和 !=
运算符:用于比较两个字符串是否相等或不相等。
string str1 = "Hello";
string str2 = "Hello";
if (str1 == str2) {
// 字符串相等
}
if (str1 != str2) {
// 字符串不相等
}
4. <
、<=
、>
、>=
运算符:用于比较两个字符串的大小关系,按字典序进行比较。
string str1 = "apple";
string str2 = "banana";
if (str1 < str2) {
// str1 在字典序上小于 str2
}
5. []
运算符:用于访问字符串中的单个字符。
string str = "Hello";
char c = str[0]; // 获取字符串的第一个字符 'H'
6. <<
运算符:用于将字符串插入到输出流中。
string str = "Hello";
cout << str; // 输出 "Hello"
7. >>
运算符:用于从输入流中读取字符串。
string str;
cin >> str; // 从标准输入读取一个字符串
这些是 string
类中常用的一些重载运算符,它们使得对字符串的操作更加方便和直观。
string
除了有重载运算符还有很多成员粗略的梗概
string
类的成员函数包括但不限于以下内容:
1. 构造函数:
string();
:默认构造函数,创建一个空的字符串。string(const char* s);
:使用以 null 结尾的 C 字符串初始化字符串。string(const string& str);
:拷贝构造函数,使用另一个字符串初始化当前字符串。string(size_t n, char c);
:使用字符c
初始化一个包含n
个字符的字符串。
2. 赋值和替换:
string& operator=(const char* s);
:将 C 字符串赋值给当前字符串。string& operator=(const string& str);
:将另一个字符串赋值给当前字符串。string& operator=(char c);
:用字符c
替换字符串中的内容。string& assign(const char* s);
:用 C 字符串赋值给当前字符串。string& assign(const string& str);
:用另一个字符串赋值给当前字符串。string& assign(size_t n, char c);
:用字符c
替换字符串中的内容,并重设字符串长度为n
。string& assign(const char* s, size_t n);
:用前n
个字符的 C 字符串赋值给当前字符串。
3. 字符串追加和连接:
string& operator+=(const char* s);
:将 C 字符串追加到当前字符串的末尾。string& operator+=(const string& str);
:将另一个字符串追加到当前字符串的末尾。string& operator+=(char c);
:将字符c
追加到当前字符串的末尾。string& append(const char* s);
:将 C 字符串追加到当前字符串的末尾。string& append(const string& str);
:将另一个字符串追加到当前字符串的末尾。string& append(const char* s, size_t n);
:将 C 字符串的前n
个字符追加到当前字符串的末尾。string& append(size_t n, char c);
:将字符c
追加n
次到当前字符串的末尾。
4. 字符访问:
char& operator[](size_t pos);
:返回字符串中指定位置的字符,并允许修改。const char& operator[](size_t pos) const;
:返回字符串中指定位置的字符,但不允许修改。char& at(size_t pos);
:返回字符串中指定位置的字符,并进行边界检查。const char& at(size_t pos) const;
:返回字符串中指定位置的字符,并进行边界检查,但不允许修改。char& front();
:返回字符串的第一个字符,并允许修改。const char& front() const;
:返回字符串的第一个字符,但不允许修改。char& back();
:返回字符串的最后一个字符,并允许修改。const char& back() const;
:返回字符串的最后一个字符,但不允许修改。
5. 字符串容量:
size_t length() const;
:返回字符串的长度。size_t size() const;
:返回字符串的长度,与length()
函数相同。size_t capacity() const;
:返回字符串的容量。void resize(size_t n);
:重新设置字符串的长度为n
,并根据需要调整容量。void resize(size_t n, char c);
:重新设置字符串的长度为n
,并根据需要调整容量,新元素初始化为字符c
。void reserve(size_t n);
:请求将字符串的容量调整为至少n
,以容纳更多的字符。void clear();
:清空字符串,将其内容设置为空。
6. 字符串操作:
const char* c_str() const noexcept;
:返回以 null 结尾的 C 字符串形式的字符串内容。const char* data() const noexcept;
:返回指向字符串内容的指针,可能不以 null 结尾。string& replace(size_t pos, size_t len, const char* s);
:用 C 字符串替换从pos
开始的len
个字符。string& replace(size_t pos, size_t len, const string& str);
:用另一个字符串替换从pos
开始的len
个字符。void swap(string& str);
:交换当前字符串与另一个字符串的内容。size_t copy(char* dest, size_t len, size_t pos) const;
:将从pos
开始的len
个字符复制到指定的目标字符数组中。
7. 字符串查找:
size_t find(const char* s, size_t pos = 0) const;
:在字符串中查找第一个匹配 C 字符串的子串,并返回其位置。size_t find(const string& str, size_t pos = 0) const;
:在字符串中查找第一个匹配另一个字符串的子串,并返回其位置。size_t find(char c, size_t pos = 0) const;
:在字符串中查找第一个匹配字符c
的子串,并返回其位置。size_t rfind(const char* s, size_t pos = npos) const;
:在字符串中反向查找第一个匹配 C 字符串的子串,并返回其位置。size_t rfind(const string& str, size_t pos = npos) const;
:在字符串中反向查找第一个匹配另一个字符串的子串,并返回其位置。size_t rfind(char c, size_t pos = npos) const;
:在字符串中反向查找第一个匹配字符c
的子串,并返回其位置。size_t find_first_of(const char* s, size_t pos = 0) const;
:在字符串中查找第一个匹配参数字符序列中任一字符的子串,并返回其位置。size_t find_first_of(const string& str, size_t pos = 0) const;
:在字符串中查找第一个匹配参数字符串中任一字符的子串,并返回其位置。size_t find_first_of(char c, size_t pos = 0) const;
:在字符串中查找第一个匹配字符c
的子串,并返回其位置。size_t find_last_of(const char* s, size_t pos = npos) const;
:在字符串中反向查找第一个匹配参数字符序列中任一字符的子串,并返回其位置。size_t find_last_of(const string& str, size_t pos = npos) const;
:在字符串中反向查找第一个匹配参数字符串中任一字符的子串,并返回其位置。size_t find_last_of(char c, size_t pos = npos) const;
:在字符串中反向查找第一个匹配字符c
的子串,并返回其位置。size_t find_first_not_of(const char* s, size_t pos = 0) const;
:在字符串中查找第一个不匹配参数字符序列中任何字符的子串,并返回其位置。size_t find_first_not_of(const string& str, size_t pos = 0) const;
:在字符串中查找第一个不匹配参数字符串中任何字符的子串,并返回其位置。size_t find_first_not_of(char c, size_t pos = 0) const;
:在字符串中查找第一个不匹配字符c
的子串,并返回其位置。size_t find_last_not_of(const char* s, size_t pos = npos) const;
:在字符串中反向查找第一个不匹配参数字符序列中任何字符的子串,并返回其位置。size_t find_last_not_of(const string& str, size_t pos = npos) const;
:在字符串中反向查找第一个不匹配参数字符串中任何字符的子串,并返回其位置。size_t find_last_not_of(char c, size_t pos = npos) const;
:在字符串中反向查找第一个不匹配字符c
的子串,并返回其位置。
8. 字符串比较:
int compare(const char* s) const noexcept;
:将当前字符串与 C 字符串进行比较。int compare(const string& str) const noexcept;
:将当前字符串与另一个字符串进行比较。bool operator==(const char* s) const noexcept;
:检查当前字符串是否与 C 字符串相等。bool operator==(const string& str) const noexcept;
:检查当前字符串是否与另一个字符串相等。
9. 字符串子串:
string substr(size_t pos = 0, size_t len = npos) const;
:返回从pos
开始的len
个字符的子串。
10. 其他:
const char* getline(istream& is, string& str, char delim);
:从输入流中读取一行字符,并将其存储到字符串中,直到遇到指定的分隔符delim
。
这些是 string
类的主要成员函数,可以用于字符串的构造、操作、查找和比较等功能。
当然,除了常用的成员函数之外,string
类还提供了一些不太常用的成员函数,包括但不限于:
1. 字符串修改:
void swap(string& str) noexcept;
:交换当前字符串与参数字符串的内容。
2. 字符串属性:
const char* c_str() const noexcept;
:返回一个指向以空字符结尾的字符数组的指针,该数组存储了与字符串等效的 C 字符串。const char* data() const noexcept;
:返回指向字符串的第一个字符的指针。
3. 字符串大小:
size_t copy(char* dest, size_t len, size_t pos = 0) const;
:将字符串的一部分复制到字符数组中。size_t max_size() const noexcept;
:返回字符串能够容纳的最大字符数。
4. 字符串查找:
size_t find(const char* s, size_t pos, size_t count) const;
:在字符串中查找子串,限制搜索范围为pos
位置开始的count
个字符。size_t find(const string& str, size_t pos, size_t count) const;
:在字符串中查找子串,限制搜索范围为pos
位置开始的count
个字符。
5. 字符串比较:
bool operator!=(const char* s) const noexcept;
:检查当前字符串是否与 C 字符串不相等。bool operator!=(const string& str) const noexcept;
:检查当前字符串是否与另一个字符串不相等。bool operator<(const char* s) const noexcept;
:检查当前字符串是否小于 C 字符串。bool operator<(const string& str) const noexcept;
:检查当前字符串是否小于另一个字符串。bool operator<=(const char* s) const noexcept;
:检查当前字符串是否小于或等于 C 字符串。bool operator<=(const string& str) const noexcept;
:检查当前字符串是否小于或等于另一个字符串。bool operator>(const char* s) const noexcept;
:检查当前字符串是否大于 C 字符串。bool operator>(const string& str) const noexcept;
:检查当前字符串是否大于另一个字符串。bool operator>=(const char* s) const noexcept;
:检查当前字符串是否大于或等于 C 字符串。bool operator>=(const string& str) const noexcept;
:检查当前字符串是否大于或等于另一个字符串。
这些函数在特定情况下可能会派上用场,但通常情况下不太常用。