String容器

string容器概念

  • char*是一个指针,string是一个类,string封装了char*,管理这个字符串,是一个char*型的容器
  • string封装了许多实用的成员方法,例如查找find,拷贝copy,删除delete,替换,插入等等
  • 不用考虑内存释放和越界,string管理char*所分配的内存,每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等
  • string和char*可以相互转换
  • s.at(i)和s[i]的区别在于s.at(i)若越界会抛出异常out_of_range

String常用的API

string拼接操作
string& operator+=(const string& str);//重载+=操作符
string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& append(const char *s);//把字符串连接到当前字符串结尾
string& append(const char *s,int n);//把字符串的前n个字符连接到当前字符串结尾
string& append(const string &s);//同operator+=()
string& append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
string& append(int n,char c);//在当前字符串结尾添加n个字符c
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=0const;//查找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
string比较操作
  • compare函数在>0时返回1,<0时返回-1,==时返回0
  • 比较区分大小写,参考字典顺序,排前面越小,大写比小写的要小
int compare(const string &s) const;//与字符串s比较
int compare(const char *s) const;//与字符串s比较
获取string子串操作
string substr(int pos=0,int n=npos) const;//返回由pos开始的n个字符组成的字符串
插入和删除操作
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个字符

小结

  • 省事

代码案例

#include<iostream>
#include<string>

using namespace std;

void test01()
{
	
	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 test02()
{
	string s1;
	string s2("appp");
	s1 = "abcdef";
	cout << s1 << endl;
	s1 = s2;
	cout << s1 << endl;
	s1 = 'a';
	cout << s1 << endl;
//成员方法
	s1.assign("jkl");
	cout << s1 << endl;
}

//赋值操作
void test03()
{
	string s1 = "abcdef";
	//重载[]操作符
	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.at(100) << endl;
		//cout << s1[100] << endl;//会宕掉
	}
	catch (...)
	{
		cout << "越界" << endl;
	}
}
//拼接操作
void test04()
{
	string s1 = "abcd";
	string s2 = "1111";
	s1 += "asd";
	s1 += s2;
	cout << s1 << endl;

	string s3 = "2222";
	s2.append(s3);
	cout << s2 << endl;

	string s4 = s2 + s3;
	cout << s4 << endl;
}

void test05()
{
	string s = "abcdefghifgjkl";
	int pos = s.find("fg");
	cout << "pos:" << pos << endl;
	pos = s.rfind("fg");
	cout << "pos:" << pos << endl;
}

//string替换
void test06()
{
	string s = "abcdeg";
	s.replace(0, 0, "123");//从0开始替换掉0个字符,即字符串首插入
	cout << s << endl;
}

//string比较
void test07()
{
	string s1 = "abcd";
	string s2 = "abce";
	int k;
	if (( k=s1.compare(s2))==0)
	{
		cout << "字符串相等" << endl;
	}
	else
	{
		cout << "字符串不相等" << endl;
	}

}

void test08()
{
	string s1 = "zbcdefg";
	string s2 = s1.substr(1, 3);
	cout << s2 << endl;
}
//插入和删除
void test09()
{
	string s = "abcdefg";
	s.insert(3, "123");
	cout << s << endl;
}

int main(void)
{
	//test01();
	//test02();
	//test03();
	//test04();
	//test05();
	//test06();
	//test07();
	//test08();
	test09();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mr_Csyn

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

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

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

打赏作者

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

抵扣说明:

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

余额充值