C++几个常用的函数,以后不用再写

1、字符串分割函数

复制代码
//字符串分割函数
vector<string> split(string str,string pattern){
    string::size_type pos;
    vector<std::string> result;
    str+=pattern;//扩展字符串以方便操作
    string::size_type size=str.size();     
    for(int i=0; i<size; i++){
        pos=str.find(pattern,i);        
        if(pos<size){
            std::string s=str.substr(i,pos-i);            
            result.push_back(s);
            i=pos+pattern.size()-1;
        }
    }
    return result;
}
复制代码

2、字符串替换函数

/***********将str中的所有fromStr子串替换为toStr子串*/
void replaceStr(string &str,string fromStr,string toStr){
    int index = 0;
    while((index = str.find(fromStr,index)) != -1){            
        int kwlen = fromStr.size();
        str = str.replace(index,kwlen,toStr);
    }
}

3、大写转小写

/********将字符串转为大写*************/
void upperAll(string &str){    
        transform(str.begin(),str.end(),str.begin(),::toupper);
    }
}

以上几个函数都需要使用标准命名空间std

posted @   CodeMeals  阅读(489)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
阅读排行:
· 几个技巧,教你去除文章的 AI 味!
· 对象命名为何需要避免'-er'和'-or'后缀
· 系统高可用的 10 条军规
· 关于普通程序员该如何参与AI学习的三个建议以及自己的实践
· AI与.NET技术实操系列(八):使用Catalyst进行自然语言处理
reliable statistics
Visitors
点击右上角即可分享
微信分享提示