2.2 C++STL string容器详解

引言

string容器
string是C++风格的字符串,而string本质上是一个类

C++ STL中的string区别于C语言风格的cstring, 对于这一点有疑惑的小伙伴推荐先看一下这篇文章:
C++笔记(cstring和string的区别)

2.2.1 string的特性

  1. Char为指针,String是一个类。string封装了char,管理这个字符串,是一个char*型的容器。

  2. 不用考虑内存释放和越界

  3. String封装了很多实用的成员方法
    查找,拷贝,删除…

2.2.2 string用法理论

2.2.2.1 string构造函数

string(); //创造一个空的字符串
string(const char*s) //使用字符串s初始化
string(const string& str); //使用一个string对象初始化另一个string对象
string(int n, char c); //使用n个字符c初始化

2.2.2.2 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赋给当前字符串

2.2.2.3 string取值操作

这里参考下面代码区text03函数

string s = "abcdefg";
	//重载[]操作符
	for (int i = 0;i < s.size(); i++)
	{
		cout << s[i] << " ";
	}
	cout << endl;
	//at成员函数
	for (int i = 0; i < s.size(); i++)
	{
		cout << s.at(i) << " ";
	}
	cout << endl;

2.2.2.4 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个字符连接到字符串结尾

2.2.2.6 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最后一次出现位置

2.2.2.7 string替换

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

2.2.2.7 string字符串比较

字符串比较是按字符的ASCII码进行对比或者说是按照字典序,字母越靠前越小。
注意:大写A比小写a小

(等于) = 返回 0

(大于) > 返回 1

(小于) < 返回 -1

int compare(const string &s) const; // 与字符串s比较
int compare(const char *s) const; //与字符串s比较

2.2.2.8 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个字符

2.2.2.9 string子串

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

string用法示例代码

//函数text0~9对应2.2.2.1~2.2.2.9
#include<iostream>
#include<string>
using namespace std;

//初始化
void text01()
{
	cout << "\ntext01\n";
	string s1;//调用无参构造
	string s2(10, 'a');
	string s3("abcdefg");
	string s4(s3);//拷贝构造

	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
}

//赋值操作
void text02()
{
	cout << "\ntext02\n";
	string s1;
	string s2("appp");
	s1 = "abcdef";
	cout << s1 << endl;
	s1 = s2;
	cout << s1 << endl;

	//成员方法
	s1.assign("jkl");
	cout << s1 << endl;
}

//取值操作
void text03()
{
	cout << "\ntext03\n";
	string s1 = "abcdefg";

	//重载[]操作符
	for (int i = 0;i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;

	//at成员函数
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1.at(i) << " ";
	}
	cout << endl;

	//区别:[]方式 如果访问越界,直接挂了
	//at方式 访问越界 抛异常out_of_range
	try
	{
		/*cout << s1[100] << endl;*/
		cout << s1.at(100) << endl;
	}
	catch (...)
	{
		cout << "越界" << endl;
	}
}

//拼接操作
void text04()
{
	cout << "\ntext04\n";
	//1
	string s = "abcd";
	s += "abcd";
	string s2 = "1111";
	s += s2;
	cout << s << endl;

	//2
	string s3 = "2222";
	s2.append(s3);
	cout << s2 << endl;
	string s4 = s2 + s3;
	cout << s4 << endl;;
}

//查找操作
void text05()
{
	cout << "\ntext05\n";
	string s = "abcdefghjkl";
	//查找第一次出现的位置
	int pos = s.find("fg");
	cout << "pos:" << pos << endl;//返回5

	//查找最后一次出现的位置
	pos = s.rfind("fg");
	cout << "pos:" << pos << endl;
}

//替换操作
void text06()
{
	cout << "\ntext06\n";
	string s = "abcdefg";
	s.replace(1,3,"111");
	cout << s << endl;
}

//字符串比较
void text07()
{
	cout << "\ntext07\n";
	string s1 = "abcd", s2 = "abce";
	if (s1.compare(s2) == 0)
	{
		cout << "相等" << endl;
	}
	else 
	{
		cout << "不相等 值为:" << s1.compare(s2) << endl;
		//ASCII码字典序,小于为-1。
	}
}

//string子串
void text08()
{
	cout << "\ntext08\n";
	string s = "abcdefg";
	string mysubstr = s.substr(1, 3);
	cout << mysubstr << endl;
}

//string插入和删除操作
void text09()
{
	cout << "\ntext09\n";
	string s = "abcdefg";
	s.insert(3, "111");
	cout << s << endl;

	s.erase(0, 2);//从0位置开始删2个元素
	cout << s << endl;
}

int main()
{
	text01();
	text02();
	text03();
	text04();
	text05();
	text06();
	text07();
	text08();
	text09();
	return 0;
}

string用法示例代码结果

测试结果


谢谢阅读(〃’ ▽ '〃)如有纰漏欢迎指出,觉得还不错就点个赞吧。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只子美

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值