关注

C++ STL之string容器


一、C与C++字符串的差别

  • C语言下的字符串char*是一个指针。
  • C++下的字符串string是一个类,类内部封装了char来管理字符串,是一个char&型的容器。

相比于C语言下以'\0'结尾中间不能含有任何'\0'的字符串,C++的string类的字符串可以存在任何东西,包括'\0'


二、string类对象的容量操作

函数名称功能说明
size返回字符串的有效长度。
length返回字符串的有效字符长度。
capacity返回空间的总大小。
empty检测字符串是否为空串,是返回true,否返回false。
clear清空有效字符。
reserve为字符串预留空间。
resize将有效字符的个数改为n个,多出的空间用字符c填充。

三、string类中的常见API

在编程中,API(Application Programming Interface)是指一组已定义的接口或函数,用于让不同的软件组件之间进行交互和通信,实现不同应用程序之间的互操作性。

总览

引用的头文件#include<string>
string的使用需用到命名空间std

string类中API的总览

1.构造

C++中的string类默认构造函数的字符串初始值是空字符串,即长度为0的字符串。

string();
//默认构造函数,创建一个空的字符串
string(const string& str);
//拷贝构造函数,使用一个string对象初始化另一个string对象
string(const char*s);
//含参构造函数,使用C风格字符串初始化
string(int n, char c);
//含参构造函数,使用n个字符c初始化

代码使用如下:

string s1;//默认构造
string s2(s1);
string s3("Hello World!");
string s4(10, 'a');//用10个'a'来初始化字符串

2.赋值

重载赋值操作符 =

string& operator=(const char* s);
//C风格字符串赋值给当前的字符串
string& operator=(const string& s):
//把字符串s赋给当前的字符串
string& operator=(const char c);
//字符赋值给当前的字符串

代码使用如下:

string s1;
string s2("Hello World!");

s1 = s2;
s1 = "Hello World!";
s1 = 'c';

成员函数 assign

string& assign(const char* s);
//C风格字符串赋值给当前的字符串
string& assign(const char* s, int n);
//把C风格字符串s的前n个字符赋给当前的字符串
string& assign(const string& s);
//把字符串s赋给当前字符串
string& assign(int n, char c);
//把n个字符c赋给当前的字符串
string& assign(const char* s, int pos, int n);
//把C风格字符串s中从start开始的n个字符赋值给当前字符串
string& assign(const string& s, int start, int n);
//将字符串s中从start开始的n个字符赋值给当前字符串
string& assign(const string& s, int start);
//将字符串s中从start开始直至结束的字符赋值给当前字符串

代码使用如下:

string s1;
string s2("Hello World!");

s1.assign("Hello World!");
s1.assign("Hello World!", 5);
s1.assign(s2);
s1.assign(14, 'c');
s1.assign("Hello World!", 6, 3);
s1.assign(s2, 6, 3);
s1.assign(s2, 6);

3.存取

重载下标获取操作符 [ ]

使用下标操作符获取字符时,如果下标越界,程序将会强制终止。

char& operator[](int n);
//通过[]下标方式获取字符

代码使用如下:

string s1("Hello World!");

s1[0];
s1[1];
s1[2];

成员函数 at

使用at方法获取字符时,如果下标越界, at方法内部会抛出异常(exception),可以使用try-catch捕获并处理该异常。

char& at(int n);
//通过at方法获取字符

代码使用如下:

string s1("Hello World!");

s1.at(0);
s1.at(1);
s1.at(2);

使用try-catch捕获并处理该异常:

#include <iostream>
using namespace std;
int main()
{
	string s = "hello world";
	try
	{
		//s[100]不会抛出异常,程序会直接挂掉
		s.at(100);
	}
	catch (out_of_range& e)
		//如果不熟悉异常类型,可以使用多态特性,catch(exception& e)
	{
		cout << e.what() << endl;
		//打印异常信息
	}
	//如果不熟悉异常类型,可以使用多态特性,catch(exception& e)
	return 0;
}

4.拼接

重载复合操作符 +=

string& operator+=(const string& str);
//将字符串str追加到当前字符串末尾
string& operator+=(const char* str);
//将C风格字符数组追加到当前字符串末尾
string& operator+=(const char c);
//将字符c追加到当前字符串末尾

代码使用如下:

string s1;
string s2("Hello World!");

s1 += s2;
s1 += "Hello World!";
s1 += 'a';

成员函数 append

string& append(const char* s);
//把C风格字符数组s连接到当前字符串结尾
string& append(const char* s, int n);
//把C风格字符数组s的前n个字符连接到当前字符串结尾
string& append(const char* s, int pos, int n);
//把C风格字符数组s中从pos开始的n个字符连接到当前字符串结尾
string& append(const string& s);
//将字符串s追加到当前字符串末尾
string& append(const string& s, int pos, int n);
//把字符串s中从pos开始的n个字符连接到当前字符串结尾
string& append(const string& s, int pos);
//将字符串s中从pos开始直至结束的字符连接到当前字符串结尾
string& append(int n, char c);
//在当前字符串结尾添加n个字符c

代码使用如下:

string s1;
string s2("Hello World!");

s1.append("Hello World!");
s1.append("Hello World!", 5);
s1.append("Hello World!", 6, 3);
s1.append(s2);
s1.append(s2, 6, 3);
s1.append(s2, 6);
s1.append(5, 'o');

5.查找

成员函数 find

当查找失败时, find方法会返回-1,-1已经被封装为string的静态成员常量string::nposo

int find(const string& str, int pos = 0) const;
//查找str在当前字符串中第一次出现的位置,从pos开始查找,pos默认为0
int find(const char* s, int pos = 0) const;
//查找C风格字符串s在当前字符串中第一次出现的位置,从pos开始查找,pos默认为0
int find( const char* s, int pos, int n) const;
//从pos位置查找s的前n个字符在当前字符串中第一次出现的位置
int find(const char c, int pos = 0) const;
//查找字符c第一次出现的位置,从pos开始查找,pos默认为0

成员函数 rfind

rfind(str, pos)的实际的开始位置是pos + str.size(),即从该位置开始 (不包括该位置字符) 向前寻找匹配项,如果有则返回字符串位置,如果没有返回string::npos

int rfind(const string& str, int pos = npos)const;
//在当前字符串中,从pos位置开始查找str的最后一次出现位置
int rfind(const char* s, int pos = npos) const;
//在当前字符串中,从pos位置开始查找s的最后一次出现位置,计数永远是从前往后
int rfind(const char* s, int pos, int n) const;
//从pos位置开始查找前n个字符,找到s最后一次出现的位置
int rfine(const char c, int pos = npos) const;
//在当前字符串中,从pos位置开始查找字符c的最后一次出现位置

关于string::npos
-1其实是size t类的最大值 (补码知识) ,所以string::npos其实表示“直到字符串结束”。

同时,可以发现string::npos和container.end()送代器其实是相同含义的,说明STL各个模块的设计是统一的。


成员函数 replace

string& replace(int pos, int n, const string& str);
//将当前字符串的从pos开始的n个字符,替换为字符串str
string& replace(int pos, int n, const char* s);
//将当前字符串的从pos开始的n个字符,替换为字符串s

代码的使用如下:
string s1;
string s2("Hello World!");

s1.append("Hi");

s1.append("Hi");
s2.replace(0, 5, s1);
cout << s2 << endl;

输出结果:

Hi World!

6.比较

成员函数 compare

  • 字符串比较是按字符的ASCII码进行对比

compare函数依据字典序比较,在当前字符串比给定字符串小时返回-1,在当前字符串比给定字符串大时返回1,相等时返回0。

在C++中,使用string类的compare函数比较两个字符串的大小时,如果短字符串是长字符串的前缀,那么结果是短字符串小于长字符串。

int compare(const string& s) const;
//与字符串s比较
int compare(const char* s) const;
//与C风格的字符数组比较

代码的使用如下:

string s1;
string s2("Hello World!");

//s1的字符串内容为"Hello"
s1.assign(s2, 0, 4);
cout << s1.compare(s2) << endl;

输出结果:

-1

重载比较操作符

bool operator<(const string& str) const;
bool operator<(const char* str) const;
bool operator<=(const string& str) const;
bool operator<=(const char* str) const;
bool operator==(const string& str) const;
bool operator==(const char* str) const;
bool operator>(const string& str) const;
bool operator>(const char* str) const;
bool operator>=(const string& str) const;
bool operator>=(const char* str) const;
bool operator!=(const string& str) const;
bool operator!=(const char* str) const;

7.子串

成员函数 substr

string substr(int pos = 0, int n = npos) const;
//返回由pos开始的n个字符组成的字符串

string::npos的含义是到整个字符串结束


代码的使用如下:

string s5(s2.substr(0, 5));
cout << s5 << endl;

输出结果:

Hello

8.插入

成员函数 insert

string& insert(int pos, const char* s); 
//在pos位置插入C风格字符数组
string& insert(int pos, const string& str);
//在pos位置插入字符串str
string& insert(int pos, int n, char c);
//在pos位置插入n个字符c

9.删除

成员函数 erase

string::npos的含义是删除到整个字符串结束。

string& erase int pos, int n = npos);
//删除从pos位置开始的n个字符

转载自CSDN-专业IT技术社区

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/qq947467490/article/details/130183703

评论

赞0

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--