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

来源:互联网 发布:eva625软件下载 编辑:程序博客网 时间:2024/06/11 18:00

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

// 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
原创粉丝点击