STl-replace_if() trim,split,replace
2013-02-06 08:14
316 查看
STl-replace_if()替换函数的妙用
分类: STL2012-10-1119: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 <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)
┊ 喜欢▼ ┊打印┊举报
已投稿到: | 排行榜 圈子 |
---|
相关文章推荐
- c++使用 STL string 实现split,trim和replace方法
- C++中STL对string进行trim,split,replace操作
- STL 中的 std::string大小写转换 lowercase、uppercase、Trim、replace、split
- stl string 的 trim split replace tolower toupper
- STL 之replace,replace_if,replace_copy,replace_copy_if
- STL 之replace,replace_if,replace_copy,replace_copy_if
- STL中remove_if()/find_if()/replace_if()的使用方法(The usages of remove_if()/find_if()/replace_if() )
- STL_算法_替换(replace、replace_copy、replace_if、replace_copy_if)
- 从零开始学C++之STL(六):变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)
- 从零开始学C++之STL(六):变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)
- STl-replace_if()替换函数的妙用
- C++ 使用STL string 实现的split,trim,replace-修订
- STL replace_if
- STL(六):变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)
- C++ 使用STL string 实现的split,trim,replace-修订
- STL常用算法: replace.replace_copy.replace_if.replace_copy_if
- C++ 使用STL string 实现的split,trim,replace-修订
- C++ 使用STL string 实现的split,trim,replace-修订
- C++ 使用STL string 实现的split,trim,replace-修订
- C++ 使用STL string 实现的split,trim,replace