您的位置:首页 > 其它

STl-replace_if() trim,split,replace

2013-02-06 08:14 316 查看
 


STl-replace_if()替换函数的妙用

分类: STL2012-10-11
19:42 82人阅读 评论(0) 收藏 举报

replace_if(beg,end,op,val)函数还是有时候有点作用的我觉得...

虽然不怎么好用, 如果是每个值都要经过特定的变换才能得到正确答案的话,,,其实可以用for_each()函数遍历然后每个做运算的

而上面的那个函数值是用作符合一定特征的所有的个体都发生变化的情况...

注意replace() 中间是需要4个参数的.

[cpp] view
plaincopy

#include <stdio.h>  

#include <string.h>  

#include <stdlib.h>  

#include <iostream>  

#include <algorithm>  

#include <time.h>  

  

  

using namespace std;  

  

  

bool op(char a)  

{  

    if(a<='z'&&a>='a')  

        return 1;  

    else return 0;  

}  

  

bool visit(char a)  

{  

    cout << a << " ";  

    return 1;  

}  

  

int main()  

{  

    char str[100];  

    scanf("%s",str);  

    int n=strlen(str);  

    replace_if(str,str+n,op,'A');  

    for_each(str,str+n,visit);  

    return 0;  

}  


C++中STL对string进行trim,split,replace操作

 (2012-05-25 09:56:12)


转载▼

标签: 


杂谈

分类: C/C
#include <iostream>

#include <vector>

using namespace std;

namespace strtool

{

string trim(const string& str)

{

    string::size_type pos = str.find_first_not_of(' ');

    if (pos == string::npos)

    {

        return str;

    }

    string::size_type pos2 = str.find_last_not_of(' ');

    if (pos2 != string::npos)

    {

        return str.substr(pos, pos2 - pos + 1);

    }

    return str.substr(pos);

}

int split(const string& str, vector<string>& ret_, string sep = ",")

{

    if (str.empty())

    {

        return 0;

    }

    string tmp;

    string::size_type pos_begin = str.find_first_not_of(sep);

    string::size_type comma_pos = 0;

    while (pos_begin != string::npos)

    {

        comma_pos = str.find(sep, pos_begin);

        if (comma_pos != string::npos)

        {

            tmp = str.substr(pos_begin, comma_pos - pos_begin);

            pos_begin = comma_pos + sep.length();

        }

        else

        {

            tmp = str.substr(pos_begin);

            pos_begin = comma_pos;

        }

        if (!tmp.empty())

        {

            ret_.push_back(tmp);

            tmp.clear();

        }

    }

    return 0;

}

string replace(const string& str, const string& src, const string& dest)

{

    string ret;

    string::size_type pos_begin = 0;

    string::size_type pos       = str.find(src);

    while (pos != string::npos)

    {

        cout <<"replacexxx:" << pos_begin <<" " << pos <<"\n";

        ret.append(str.data() + pos_begin, pos - pos_begin);

        ret += dest;

        pos_begin = pos + 1;

        pos       = str.find(src, pos_begin);

    }

    if (pos_begin < str.length())

    {

        ret.append(str.begin() + pos_begin, str.end());

    }

    return ret;

}

}

 

int main(int argc, char* argv[])

{

    cout << strtool::trim(" nihao ") <<"\n";

    vector<string> vt;

    strtool::split(",o h,,,nice,,,,,,,", vt);

    for (size_t i = 0; i < vt.size(); ++ i)

    {

        cout <<"out:" << vt[i] <<"\n";

    }

    string ret = strtool::replace("xxAxxxAxxAxx", "A", "B");

    cout <<"replace:" << ret <<"\n";

    return 0;

}

分享: 

分享到新浪Qing 

0


喜欢

阅读(75)┊ 评论 (0)┊
收藏(0) ┊转载(0)

┊ 喜欢▼ ┊打印举报

已投稿到:
 排行榜

 圈子
前一篇:MFC显示png图片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: