[C/C++]_[初级]_[标准库之几种算法库函数的运用,如字符串查找(字符个数查找)、分隔、删除、替换、排序、字符交换位置、大小写转换等等]

场景:在处理一些复杂的字符时候,我们要对其进行相应的处理才能得到我们想要的结果,包括:文件目录的提取,文件后缀的提取,提取某一个范围内的字符串,不符合规则字符的删除和替换等等。

// test_arithmetic.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <algorithm>//必须加上
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	std::string str ="12345674849dfsfds";
	cout<<"str:"<<str<<endl;

	//1字符位置的交换
	iter_swap(str.begin(),str.begin()+6);
	cout<<"swpa:"<<str<<endl;
	//2.字符串中查找字符
	bool value = binary_search(str.begin(),str.end(),'4');
	cout<<"value:"<<value<<endl;
	//3.字符串中查找字符存在的个数
	int items =count(str.begin(),str.end(),'4');
	cout<<"items:"<<items<<endl;
	
	//4字符串中字符替换
	replace(str.begin(),str.end(),'4','a');
	cout<<"replace:"<<str<<endl;

	//5字符串中字符删除
	str.erase(remove(str.begin(),str.end(),'a'),str.end());
	cout<<"remove:"<<str<<endl;
	
	//6字符串自动排序
	sort(str.begin(),str.end());
	cout<<"sort:"<<str<<endl;

	//7.字符串大小写转换
	std::string str ="SKJH|FJH|jhds|fjh";
	transform(str.begin(),str.end(),str.begin(),::tolower);
	cout<<"lower:"<<str<<endl;
	transform(str.begin(),str.end(),str.begin(),::toupper);
	cout<<"upper:"<<str<<endl;

	//8分隔字符串
	vector<std::string> arr;
	std::stringstream ss(str);
	std::string splitStr;
	while(getline(ss,splitStr,'|'))
	{
		arr.push_back(splitStr);
	}
	for(vector<std::string>::iterator it = arr.begin();it!= arr.end();++it)
	{
		cout<<"it:"<<(*it)<<endl;
	}
	system("pause");
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值