【C++】:STL详解 —— string类

目录

string的概念

string的构造函数

string的大小

size() 和 length()

empty() 

string的插入

push_back 函数

insert 函数 

string的删除 

pop_back 函数(C++11)

erase 函数

clear 函数

string的拼接

+= 运算符

append() 函数 

string的替换

replace() 函数 

string的查找

find()函数

rfind()函数

string的比较

运算符比较

compare 函数

string的交换

swap 函数

string中元素的访问

[]+下标

at 函数

迭代器

begin 函数、end 函数

rbegin 函数、rend 函数 

string转换为字符串

c_str 函数、data 函数 

string中子字符串的提取

substr 函数

copy 函数


string的概念

string 是 C++ 标准库中用于处理字符串的类,封装了字符串的常见操作,如拼接、查找、替换等,同时自动管理内存,避免了 C 风格字符数组的繁琐操作。

string的构造函数

string();                          //构造一个空字符串

string(const char* s);             //复制s所指的字符序列

string(const char* s, size_t n);   //复制s所指字符序列的前n个字符

string(size_t n, char c);          //生成n个c字符的字符串

string(const string& str);         //生成str的复制品

string(const string& str, size_t pos, size_t len = npos);  //复制str中从字符位置pos开始并跨越len个字符的部分
string s1;                   // 默认构造
string s2("Hello");          // C 字符串构造
string s3("Hello", 5);       // C 字符串所指的前n个字符
string s4(5, 'X');           // 重复字符
string s5(s2);               // 拷贝构造
string s6(s2, 2);            // 子串构造(从索引2到末尾)
string s7(s2, 1, 3);         // 子串构造(从索引1取3字符)

string的大小

size() 和 length()

二者完全等价,返回字符串的字符个数:

string s = "Hello";
cout << s.size();   // 输出 5
cout << s.length(); // 输出 5

empty() 

判断字符串是否为空:

string s;
if (s.empty()) {
    cout << "字符串为空";
}

string的插入

push_back 函数

在尾部插入字符

void push_back (char c);

insert 函数 

在指定位置插入字符串、字符或其他内容。

插入字符串

string& insert (size_t pos, const char* s);
string s = "Hello";
s.insert(5, " World");  // 在索引5处插入 " World"
cout << s << endl;      // 输出 "Hello World"

插入子串

 插入另一个字符串的某一部分:

string s = "ABCDEF";
string sub = "XYZ";
s.insert(2, sub, 1, 2); // 在索引2处插入sub的[1,2)部分("YZ")
cout << s << endl;      // 输出 "ABYZCDEF"

插入字符

插入多个重复字符:

string s = "Hello";
s.insert(3, 3, '!');    // 在索引3处插入3个'!'
cout << s << endl;      // 输出 "Hel!!!lo"

使用迭代器插入

通过迭代器指定插入位置:

iterator insert (iterator p, char c);               // 迭代器
string s = "abcde";
auto it = s.begin() + 2;
s.insert(it, 'X');      // 在迭代器位置插入字符'X'
cout << s << endl;      // 输出 "abXcde"

string的删除 

pop_back 函数(C++11)

删除最后一个字符。

string s = "Hello";
s.pop_back();           // s = "Hell"

erase 函数

删除指定位置或范围的字符。

删除单个字符

string s = "Hello";
s.erase(1);             // 从索引1删除到末尾,s = "H"

删除指定范围

string s = "Hello World";
s.erase(5, 6);          // 从索引5删除6个字符,s = "Hello"

使用迭代器删除

string s = "abcdef";
auto it = s.begin() + 2;
s.erase(it);            // 删除迭代器指向的字符,s = "abdef"

 删除迭代器范围

string s = "abcdef";
auto start = s.begin() + 1;
auto end = s.begin() + 4;
s.erase(start, end);    // 删除 [start, end),s = "aef"

clear 函数

清空整个字符串:

string s = "Hello";
s.clear();              // s = ""

string的拼接

+= 运算符

直接追加字符串、字符或 C 风格字符串到当前字符串末尾:

string s = "Hello";
s += " World";      // 追加字符串
s += '!';           // 追加字符
s += "123";         // 追加 C 字符串
cout << s << endl;  // 输出 "Hello World!123"

append() 函数 

更灵活的拼接方式,支持多种重载形式:

string s = "Hello";

// 追加字符串
s.append(" World");       // s = "Hello World"

// 追加另一个 string 的子串
string sub = "XYZ";
s.append(sub, 1, 2);      // 追加 sub 的 [1,2) 部分("YZ"),s = "Hello WorldYZ"

// 追加重复字符
s.append(3, '!');         // 追加 3 个 '!',s = "Hello WorldYZ!!!"

string的替换

replace() 函数 

视为先删除后插入的复合操作

替换指定范围的字符

string s = "Hello World";
s.replace(6, 5, "C++"); // 从索引6开始替换5个字符为"C++"
cout << s << endl;      // 输出 "Hello C++"

使用迭代器替换

string s = "abcde";
auto start = s.begin() + 1;
auto end = s.begin() + 3;
s.replace(start, end, "XYZ"); // 替换 [1,3) 为 "XYZ",s = "aXYZde"

string的查找

find()函数

正向查找,查找子串或字符的首次出现位置,未找到返回 string::npos

size_t find(const string& substr, size_t pos = 0) const;
size_t find(const char* substr, size_t pos = 0) const;
size_t find(char ch, size_t pos = 0) const;

示例

string s = "Hello World! Hello C++";

// 查找子串
size_t pos1 = s.find("Hello");     // pos1 = 0
size_t pos2 = s.find("World");     // pos2 = 6
size_t pos3 = s.find("Java");      // pos3 = string::npos

// 查找字符
size_t pos4 = s.find('!');         // pos4 = 11

// 从指定位置开始查找
size_t pos5 = s.find("Hello", 5);  // 从索引5开始查找,pos5 = 13

rfind()函数

反向查找,查找子串或字符的最后一次出现位置,未找到返回 string::npos

string的比较

运算符比较

运算符 说明
== 判断两个字符串是否相等
!= 判断两个字符串是否不相等
< 判断左侧字符串是否字典序小于右侧
> 判断左侧字符串是否字典序大于右侧
<= 判断左侧字符串是否小于或等于右侧
>= 判断左侧字符串是否大于或等于右侧

compare 函数

int compare (const string& str) const;
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;
  • 返回 0:字符串相等。

  • 返回正数:当前字符串字典序大于参数字符串。

  • 返回负数:当前字符串字典序小于参数字符串。

示例代码:

string s1 = "apple";
string s2 = "banana";
string s3 = "APPLE";

// 比较整个字符串
cout << s1.compare(s2) << endl;        // 负数("apple" < "banana")
cout << s1.compare("apple") << endl;   // 0(相等)

// 比较子串
cout << s1.compare(0, 3, "app") << endl;        // 0("app" == "app")
cout << s1.compare(1, 3, s2, 1, 3) << endl;     // "ppl" vs "ana" → 正数

// 与 C 风格字符串比较
cout << s1.compare("apple") << endl;   // 0

string的交换

swap 函数

void swap (string& x, string& y);
void swap (string& str);

string中元素的访问

[]+下标

char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;

at 函数

char& at (size_t pos);
const char& at (size_t pos) const;

迭代器

begin 函数、end 函数

  • begin函数:返回一个指向字符串第一个字符的迭代器
  • end函数:返回一个指向字符串结束字符的迭代器,即’\0’
 iterator begin();
 const_iterator begin() const;

 iterator end();
 const_iterator end() const;

rbegin 函数、rend 函数 

  • rbegin函数:返回指向字符串最后一个字符的反向迭代器。
  • rend函数:返回指向字符串第一个字符前面的理论元素的反向迭代器
 iterator rbegin();
 const_iterator rbegin() const;

 iterator rend();
 const_iterator rend() const;

string转换为字符串

c_str 函数、data 函数 

const char* c_str() const;
const char* data() const;
  • 在C++98中,c_str()返回 const char* 类型,返回的字符串会以空字符结尾
  • 在C++98中,data()返回 const char* 类型,返回的字符串不以空字符结尾
  • 但是在C++11版本中,c_str()与data()用法相同

 

string中子字符串的提取

substr 函数

string substr (size_t pos = 0, size_t len = npos) const;

copy 函数

size_t copy (char* s, size_t len, size_t pos = 0) const;

你可能感兴趣的:(重制C++版,c++,开发语言)