C++ string的操作及常用算法

C++ string的操作及常用算法

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	string s1 = "It's a string!";
	string s2("string s2!");
	string s3(20, 'v');
	string s4 = s1;//通过复制构造函数来初始化对象s4

	cout << "s1: " << s1 << endl;
	cout << "s2: " << s2 << endl;
	cout << "s3: " << s3 << endl;
	cout << "s4: " << s4 << endl << endl;

	//1 string的遍历
	//1.1 数组方式
	for (int i = 0; i < s1.length(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;
	//1.2 迭代器
	for (string::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	try
	{
		for (int i = 0; i < s1.length() + 3; i++)
		{
			cout << s1.at(i) << " ";//抛出异常
		}
		cout << endl;
	}
	catch (...)//捕捉所有异常
	{
		cout << "发生异常" << endl;
	}

	//2 字符指针和string的转换
	//2.1 s2===>char*
	printf("s2: %s\n", s2.c_str());
	
	//2.2 char*===>string
	const char* p = "I'm char* p";
	string s5;
	s5 = p;
	cout << "s5: " << s5 << endl;

	//2.3 s2的内容拷贝到buf中
	char buf[128] = { 0 };
	s2.copy(buf, 3, 0); //注意:只给你copy3个字符,不会变成c风格的字符串
	cout << "buf: " << buf << endl;

	//3 字符串的连接
	//3.1
	s1 = s1 + s2;
	cout << "s1: " << s1 << endl;

	//3.2
	s3.append(s4).append(s3);
	cout << "s3: " << s3 << endl;

	//4 字符串的查找和替换
	string s6 = "web hello web 111 web 222 web 333";
	//查找第一次出现web的index
	int index = s6.find("web", 0);//位置下标从0开始
	cout << "index: " << index << endl << endl;

	//案例1: 求web出现的次数
	int num = 0;
	int offIndex = s6.find("web", 0);
	while (offIndex != s6.npos)
	{
		cout << "offIndex: " << offIndex << endl;
		num++;
		offIndex++;
		offIndex = s6.find("web", offIndex);
	}
	cout << "web出现的次数为: " << num << endl << endl;

	//案例2 把小写web换成大写WEB
	offIndex = s6.find("web", 0);
	while (offIndex != s6.npos)
	{
		//cout << "offIndex: " << offIndex << endl;
		s6.replace(offIndex, 3, "WEB");//从offIndex位置开始,将后面3个字符替换为WEB
		offIndex++;
		offIndex = s6.find("web", offIndex);
	}
	cout << "s6替换后的结果: " << s6 << endl << endl;

	//截断(区间删除)和插入
	string s7 = "nanixuewanlema";
	string::iterator it2 = find(s7.begin(), s7.end(), 'n');
	cout << "s7: " << s7 << endl;
	while(it2 != s7.end())
	{
		s7.erase(it2);
		it2 = find(s7.begin(), s7.end(), 'n');
	}
	cout << "s7删除n后的结果: " << s7 << endl;

	s7.insert(0, "AAA");//头插
	s7.insert(s7.length(), "VVV");//尾插
	cout << "s7插入操作后的结果: " << s7 << endl << endl;

	//大小写替换
	string s8 = "sahjAASdjkafhFHJDJH";
	transform(s8.begin()+1, s8.end(), s8.begin()+1, toupper);//
	cout << "s8从第2个字符开始全部大写: " << s8 << endl;
	transform(s8.begin(), s8.end(), s8.begin(), tolower);
	cout << "s8全部小写: " << s8 << endl;

	return 0;
}

string的常用算法包括查找(find(起始节点下标, 终止节点下标, ‘查找元素’))、替换(replace(替换起点下标, 替换起点后n个元素, ‘替换内容’))、插入(采用头插法insert(插入结点下标, “插入的内容”))、删除(erase(迭代器)或erase(删除起点下标, 删除起点后n个元素))、大小写转换(transform(起点,终点,转换后存入位置,toupper或tolower)),此外,还有swap、reserve、resize、push_back、pop_back等诸多函数,可以满足所有关于字符串的需求。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

banjitino

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

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

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

打赏作者

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

抵扣说明:

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

余额充值