***** 标准库算法 std::replace(std::string.begin(),std::string.end(),"'","''"); 失败

hzhxxx 2005-11-26 09:16:20

我要替换一个字符串中所有的 ' 为 ''

#include <algrothm>
#include <string>

void main()
{

std::string str = "aa'bb'cc";
printf("source:%s\n",str.c_str());
std::replace(str.begin(),str.end(),"'","''");
printf("dect:%s\n",str.c_str());

}
...全文
2902 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzhxxx 2005-11-28
  • 打赏
  • 举报
回复



错了,你把单引号' 搞成 " 了,是应该把 ' 搞成 ''

回复人: xlsue(小林 不喜欢编程,只喜欢美女) ( ) 信誉:100





hzhxxx 2005-11-28
  • 打赏
  • 举报
回复


强,就是

回复人: xlsue(小林 不喜欢编程,只喜欢美女) ( ) 信誉:100

xlsue 2005-11-28
  • 打赏
  • 举报
回复
把太明白楼上的意思,这个程序就是柄'->"啊,把你可以测试一下.如果搞错了,你可以改过来嘛,懂得这个意思就行了...
xlsue 2005-11-27
  • 打赏
  • 举报
回复
//这样行吗?
#include <algorithm>
#include <string>
#include <functional>

int main()
{

std::string str = "aa'bb'cc";
printf("source:%s\n",str.c_str());
std::replace_if(str.begin(),str.end(),bind2nd(std::equal_to<char>(),'\''),'\"');
printf("dect:%s\n",str.c_str());


return 0;

}
Kid4you 2005-11-27
  • 打赏
  • 举报
回复
标准STL里面有替换一个string内部的字符串的函数么?没发现...
boost&SGI的STL都不错~
hzhxxx 2005-11-27
  • 打赏
  • 举报
回复

个人认为还是没有 stl 里面得算法实现快啊
fangrk 2005-11-27
  • 打赏
  • 举报
回复
效率低不低要测试的,你测试过了吗?
hzhxxx 2005-11-27
  • 打赏
  • 举报
回复


有不有不用循环得啊,我考虑效率问题,这样很低啊
想来想去还是算法库中得快啊
SnowwhiteYqw 2005-11-26
  • 打赏
  • 举报
回复
但是你定义的是一个string类型的对象。它的元素是char类型的,如果用replace的话,只可以把它的一个字符用另外的一个字符代替。这是replace的用法
replace()
template< class ForwardIterator, class Type >
void
replace( ForwardIterator first, ForwardIterator last,
const Type& old_value, const Type& new_value );

replace() replaces all instances of old_value with new_value within the range specified by the iterator pair [first, last).

hzhxxx 2005-11-26
  • 打赏
  • 举报
回复

回复人: fangrk(加把油,伙计!) ( ) 信誉:125

程序可以,但是慢,我假如在一个通讯程序中处理大批的字符串,是效率很低的


回复人: SnowwhiteYqw(漫步者) ( ) 信誉:100
理解错误,根本不是替换一个字符串,是一个串中的一些

假如使用循环,效率低






SnowwhiteYqw 2005-11-26
  • 打赏
  • 举报
回复
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{

std::string str = "aa'bb'cc";
cout << str << endl;
replace( str.begin(), str.end(), '\'', '*' );
cout << str << endl;
return 0;
}

replace 是对指定的每个旧元素用新元素来代替。
你上面的是字符串,不对,必须是字符。我想到一个笨的方法:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

int main()
{

vector<string> svec;
svec.push_back("aa");
svec.push_back("'");
svec.push_back("bb");
svec.push_back("'");
svec.push_back("cc");
replace( svec.begin(), svec.end(), (string)"'", (string)"''" );
for( int i = 0; i < svec.size(); ++ i )
cout << svec[i];
cout << endl;
return 0;
}

fangrk 2005-11-26
  • 打赏
  • 举报
回复
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{

string str = "aa'bb'cc";
cout<<str<<endl;
string::size_type p=str.find('\'');
while(p!=string::npos){
str.replace(p,1,"''");
p=str.find('\'',p+2);
}
cout<<str<<endl;
}
ma100 2005-11-26
  • 打赏
  • 举报
回复
#include <algrothm>
无此文件

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧