C++核心编程之STL_string容器
C++ string容器详解:从基础到高级用法
1. string容器的基本概念
1.1 本质
string
是C++标准库中的一个类,用于表示和操作字符串。与C语言中的 char*
不同,string
是一个封装了 char*
的类,提供了丰富的成员函数来操作字符串,并且自动管理内存,避免了手动管理内存带来的问题。
1.2 string与char*的区别
- char*:C语言中的字符串表示方式,本质上是一个指向字符数组的指针,需要手动管理内存,容易引发内存泄漏、越界等问题。
- string:C++中的字符串类,封装了
char*
,自动管理内存,提供了丰富的字符串操作方法,使用更加安全和方便。
1.3 string的特点
- 自动内存管理:
string
类内部自动管理字符串的内存分配和释放,用户无需关心内存问题。 - 丰富的成员函数:
string
提供了大量的成员函数,如查找、拷贝、删除、替换、插入等操作,极大地方便了字符串的处理。 - 安全性:
string
类避免了char*
常见的内存越界、缓冲区溢出等问题。
2. string的构造函数
string
类提供了多种构造函数,方便用户根据不同的需求创建字符串对象。
2.1 常用构造函数
string(); //
创建一个空的字符串。string(const char* s); //
使用C风格字符串初始化string
对象。string(const string& str); //
使用另一个string
对象初始化当前对象。string(int n, char c); //
使用n
个字符c
初始化字符串。
2.2 示例代码
#include<iostream>
using namespace std;
#include<string>
// string构造函数
/*
* string(); //创建一个空的字符串 例如: string str;
* string(const char* s); //使用字符串s初始化
* string(const string& str); //使用一个string对象初始化另一个string对象
* string(int n, char c); //使用n个字符c初始化
*/
void test01()
{
string s1; // 默认构造
const char* str = "hello world";
string s2(str);
cout<<"s2= "<<s2<<endl;
string s3(s2);
cout<<"s3= "<<s3<<endl;
string s4(10, 'a');
cout<<"s4= "<<s4<<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
2.3 总结
string
的构造函数非常灵活,可以根据不同的需求选择合适的构造函数来初始化字符串对象。
3. string的赋值操作
string
类提供了多种赋值方式,方便用户对字符串进行赋值操作。
3.1 常用赋值函数
string& operator=(const char* s); //
将C风格字符串赋值给当前字符串。string& operator=(const string &s); //
将另一个string
对象赋值给当前字符串。string& operator=(char c); //
将单个字符赋值给当前字符串。string& assign(const char *s); //
将C风格字符串赋值给当前字符串。string& assign(const char *s, int n); //
将C风格字符串的前n
个字符赋值给当前字符串。string& assign(const string &s); //
将另一个string
对象赋值给当前字符串。string& assign(int n, char c); //
将n
个字符c
赋值给当前字符串。
3.2 示例代码
#include<iostream>
using namespace std;
#include<string>
// string赋值操作
/*
* string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
* string& operator=(const string &s); //把字符串s赋给当前的字符串
* string& operator=(char c); //字符赋值给当前的字符串
* string& assign(const char *s); //把字符串s赋给当前的字符串
* string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
* string& assign(const string &s); //把字符串s赋给当前字符串
* string& assign(int n, char c); //用n个字符c赋给当前字符串
*/
void test01()
{
string str1;
str1 = "hello world";
cout<<"str1 = "<<str1<<endl;
string str2;
str2 = str1;
cout<<"str2 = "<<str2<<endl;
string str3;
str3 = 'o';
cout<<"str3 = "<<str3<<endl;
string str4;
str4.assign("hello C++");
cout<<"str4 = "<<str4<<endl;
string str5;
str5.assign("hello C++", 5);
cout<<"str5 = "<<str5<<endl;
string str6;
str6.assign(str4);
cout<<"str6 = "<<str6<<endl;
string str7;
str7.assign(10, 'o');
cout<<"str7 = "<<str7<<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.3 总结
string
的赋值操作非常灵活,常用的 operator=
和 assign
函数可以满足大多数需求,但是operator=是比较实用的。
4. string字符串拼接
string
类提供了多种拼接字符串的方式,方便用户在字符串末尾添加内容。
4.1 常用拼接函数
string& operator+=(const char* str); //
将C风格字符串拼接到当前字符串末尾。string& operator+=(const char c); //
将单个字符拼接到当前字符串末尾。string& operator+=(const string& str); //
将另一个string
对象拼接到当前字符串末尾。string& append(const char *s); //
将C风格字符串拼接到当前字符串末尾。string& append(const char *s, int n); //
将C风格字符串的前n
个字符拼接到当前字符串末尾。string& append(const string &s); //
将另一个string
对象拼接到当前字符串末尾。string& append(const string &s, int pos, int n); //
将另一个string
对象从pos
开始的n
个字符拼接到当前字符串末尾。
4.2 示例代码
#include<iostream>
using namespace std;
#include<string>
// string 字符串拼接 在字符串末尾进行拼接
/*
* string& operator+=(const char* str); //重载+=操作符
* string& operator+=(const char c); //重载+=操作符
* string& operator+=(const string& str); //重载+=操作符
* string& append(const char *s); //把字符串s连接到当前字符串结尾
* string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
* string& append(const string &s); //同operator+=(const string& str)
* string& append(const string &s, int pos, int n); //字符串s中从pos开始的n个字符连接到字符串结尾
*/
void test01()
{
string str1 = "我";
str1 += "爱打steam";
cout<<"str1 = "<<str1<<endl;
str1 += ':';
cout<<"str1 = "<<str1<<endl;
string str2 = "CF,王者荣耀";
str1 += str2;
cout<<"str1 = "<<str1<<endl;
string str3 = "我";
str3.append("喜欢");
cout<<"str3 = "<<str3<<endl;
str3.append("打游戏真的好玩", 6);
cout<<"str3 = "<<str3<<endl;
str3.append(str2);
cout<<"str3 = "<<str3<<endl;
str3.append(str2, 0, 2);
cout<<"str3 = "<<str3<<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4.3 总结
string
的拼接操作非常灵活,常用的 operator+=
和 append
函数可以满足大多数需求。
5. string查找和替换
string
类提供了查找和替换字符串的功能,方便用户对字符串进行操作。
5.1 常用查找和替换函数
int find(const string& str, int pos = 0) const; //
从pos
位置开始查找字符串str
,返回第一次出现的位置。int find(const char* s, int pos = 0) const; //
从pos
位置开始查找C风格字符串s
,返回第一次出现的位置。int find(const char c, int pos = 0) const; //
从pos
位置开始查找字符c
,返回第一次出现的位置。int rfind(const string& str, int pos = npos) const;//
从pos
位置开始查找字符串str
,返回最后一次出现的位置。int rfind(const char* s, int pos = npos) const; //
从pos
位置开始查找C风格字符串s
,返回最后一次出现的位置。int rfind(const char c, int pos = 0) const; //
从pos
位置开始查找字符c
,返回最后一次出现的位置。string& replace(int pos, int n, const string& str);//
从pos
位置开始替换n
个字符为字符串str
。string& replace(int pos, int n, const char* s); //
从pos
位置开始替换n
个字符为C风格字符串s
。
5.2 示例代码
#include<iostream>
using namespace std;
#include<string>
// string字符串查找和替换
/*
* int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
* int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找
* int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置
* int find(const char c, int pos = 0) const; //查找字符c第一次出现位置
* int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,从pos开始查找
* int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找
* int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置
* int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
* string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
* string& replace(int pos, int n,const char* s); //替换从pos开始的n个字符为字符串s
*/
// 1.查找
void test01()
{
string str1 = "abcdhijklmstjkyz";
int pos = str1.find("jk");
if(pos == -1)
{
cout<<"未找到字符串"<<endl;
}
else
{
cout<<"找到字符串, pos="<<pos<<endl;
}
//rfind 和 find 区别
//rfind从右往左查找,find从左往右查找
int pos1 = str1.rfind("jk");
cout<<"pos="<<pos1<<endl;
}
// 2.替换
void test02()
{
string str1 = "abcdhijklmstjkyz";
str1.replace(1,3,"1111");
cout<<"str1 = "<<str1<<endl;
}
int main()
{
// test01();
test02();
system("pause");
return 0;
}
5.3 总结
string
的查找和替换功能非常强大,常用的 find
和 replace
函数可以满足大多数需求。
- find查找是从左往后,rfind从右往左
- find找到字符串后返回查找的第一个字符位置,找不到返回-1
- replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串
6. string字符串比较
string
类提供了字符串比较的功能,方便用户比较两个字符串的大小。
6.1 常用比较函数
int compare(const string &s) const; //
与字符串s
进行比较。int compare(const char *s) const; //
与C风格字符串s
进行比较。
6.2 示例代码
#include<iostream>
using namespace std;
#include<string>
// string字符串比较
/*
比较方式:字符串比较是按字符的ASCII码进行对比
= 返回 0
> 返回 1
< 返回 -1
* int compare(const string &s) const; //与字符串s比较
* int compare(const char *s) const; //与字符串s比较
*/
void test01()
{
string str1 = "hello";
string str2 = "hello";
if(str1.compare(str2)==0)
{
cout<<"str1 = str2"<<endl;
}
else if(str1.compare(str2)>0)
{
cout<<"str1 > str2"<<endl;
}
else
{
cout<<"str1 < str2"<<endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
6.3 总结
string
的比较操作主要用于判断两个字符串是否相等,判断大小关系的意义不大。
7. string字符存取
string
类提供了两种方式来存取单个字符。
7.1 常用字符存取函数
char& operator[](int n); //
通过[]
操作符存取字符。char& at(int n); //
通过at
函数存取字符。
7.2 示例代码
#include<iostream>
using namespace std;
#include<string>
// string字符存取
/* string中单个字符存取方式有两种
char& operator[](int n); //通过[]方式取字符
char& at(int n); //通过at方法获取字符
*/
void test01()
{
string str1 = "hello";
// cout<<"str1 = "<<str1<<endl;
// 1.通过[]访问单个字符
for(int i=0; i<str1.size();i++)
{
cout<<str1[i]<<" ";
}
cout<<endl;
// 2.通过at访问单个字符
for(int i=0;i<str1.size();i++)
{
cout<<str1.at(i)<<" ";
}
cout<<endl;
// 修改单个字符
str1[0] = 'b';
cout<<"str1 = "<<str1<<endl;
str1.at(1) = 'a';
cout<<"str1 = "<<str1<<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
7.3 总结
string
的字符存取操作非常方便,常用的 []
和 at
函数可以满足大多数需求。
8. string插入和删除
string
类提供了插入和删除字符的功能,方便用户对字符串进行操作。
8.1 常用插入和删除函数
string& insert(int pos, const char* s); //
在pos
位置插入C风格字符串s
。string& insert(int pos, const string& str); //
在pos
位置插入另一个string
对象。string& insert(int pos, int n, char c); //
在pos
位置插入n
个字符c
。string& erase(int pos, int n = npos); //
从pos
位置开始删除n
个字符。
8.2 示例代码
#include<iostream>
using namespace std;
#include<string>
// string字符串插入和删除
/*
* string& insert(int pos, const char* s); //插入字符串
* string& insert(int pos, const string& str); //插入字符串
* string& insert(int pos, int n, char c); //在指定位置插入n个字符c
* string& erase(int pos, int n = npos); //删除从Pos开始的n个字符
*/
void test01()
{
string str = "hello";
// 插入
str.insert(1, "111");
cout<<"str = "<<str<<endl;
// 删除
str.erase(1, 3);
cout<<"str = "<<str<<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
8.3 总结
string
的插入和删除操作非常灵活,常用的 insert
和 erase
函数可以满足大多数需求。
9. string子串
string
类提供了获取子串的功能,方便用户从字符串中提取部分内容。
9.1 常用子串函数
string substr(int pos = 0, int n = npos) const; //
返回从pos
位置开始的n
个字符组成的子串。
9.2 示例代码
#include<iostream>
using namespace std;
#include<string>
// string 子串获取
/*
string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
*/
void test01()
{
string str = "abcdef";
string subStr = str.substr(1, 3);
cout<<"subStr = "<<subStr<<endl;
}
// 实用操作
void test02()
{
string email = "wangwu@sina.com";
//从邮件中 获取 用户名信息
int pos = email.find("@");
string userName = email.substr(0, pos);
cout<<"姓名:"<<userName<<endl;
}
int main()
{
// test01();
test02();
system("pause");
return 0;
}
9.3 总结
string
的子串操作非常实用,常用的 substr
函数可以满足大多数需求。
10. 总结
string
类是C++中处理字符串的强大工具,提供了丰富的成员函数来操作字符串。通过本文的介绍,相信大家对 string
的基本概念、构造函数、赋值操作、字符串拼接、查找和替换、字符串比较、字符存取、插入和删除、子串操作等有了更深入的了解。在实际开发中,灵活运用这些功能可以大大提高代码的效率和可读性。