搜索和替换字符串的算法 [英] Search and replace algorithm for string

查看:51
本文介绍了搜索和替换字符串的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我已经搜索过以前的帖子,似乎有一些

搜索的例子,并取代了所有的出现字符串

另一个字符串。


我原以为下面的代码可以工作......


string gsub(const string& sData,

const string& sFrom,

const string& sTo)

{

string sNew = sData;


if(!sNew.empty())

{

std :: replace(sData.begin(),sData.end(),

sFrom,

sTo);


}

返回sNew;

}


但是,它会产生以下编译错误:


/usr/include/c++/3.2/bits/stl_algo.h:函数`void

std :: replace(_ForwardIter,_ForwardIter,const _Tp&,const _Tp&)[with

_ForwardIter = __gnu_cxx :: __ normal_iterator< const char *,

std :: basic_string< char,std :: char_traits&l t; char> ;,std :: allocator< char> >

,_ Tp = std :: basic_string< char,std :: char_traits< char>,
std :: allocator< char> >]'':



str_tst.cpp:132:从这里实例化

/usr/include/c++/3.2/bits/stl_algo.h: 809:不匹配`const char& ==

const std :: basic_string< char,std :: char_traits< char>,

std :: allocator< char> >&''运算符

str_tst.cpp:132:从这里实例化

/usr/include/c++/3.2/bits/stl_algo.h:810:赋值只读

位置

/usr/include/c++/3.2/bits/stl_algo.h:810:无法转换`const

std :: basic_string< char,std :: char_traits< char> ;,std :: allocator< char> >''
分配给'const char''



我在这里做错了什么?看一下功能签名,它应该是
工作。看不出为什么尝试转换。


问候,


Michael

解决方案
" M" < wh*@somewhere.com>在消息中写道

新闻:TD ****************** @ news-server.bigpond.net.au ...
< blockquote class =post_quotes>我原以为下面的代码可以工作......

string gsub(const string& sData,
const string& sFrom, const string& sTo)
{
string sNew = sData;

if(!sNew.empty())

std :: replace(sData.begin(),sData.end(),
sFrom,
sTo);

}
返回sNew;
}




除非您将对std :: replace的调用更改为使用sNew而不是sData。




Andrew Koenig写道:

" M" < wh*@somewhere.com>在消息中写道
新闻:TD ****************** @ news-server.bigpond.net.au ...
< SNIP>除非您将对std :: replace的调用更改为使用sNew而不是sData。


嗨安德鲁,


不行,我仍然是同样的问题(抱歉上面虽然,只是

更改了一些变量名称。)


这是测试代码:


#include< iostream>

#include< algorithm>

#include< string>


使用命名空间std;


string gsub(const string& sData,

const string& sFrom,

const string& sTo)

{

string sNew = sData;

if(!sNew.empty())

{

std :: replace(sNew .begin(),sNew.end(),

sFrom,

sTo);

}

返回sNew;

}

int main(无效)

{

const string sData =" some text" ;

string sFrom =" text";

string sTo =" information";


cout<< 原文:"

<< sData

<< " \ n"

<< 搜索和替换之后:

<< gsub(sData,sFrom,sTo)

<< endl;


返回0;

}

以上编译错误是在linux上产生的,以下错误在

aix


" / usr /.../ include / algorithm",line 243.25:1540-0218(S)电话确实

不匹配operator ==的任何参数列表。

" / usr /.../ include / utility",第77.14行:1540-1283(I)" template < class

_T1,class _T2> std :: operator ==(const pair< _T1,_T2>&,const

pair< _T1,_T2>&)"不是一个可行的候选人。

" / usr /.../ include / xutility",第384.14行:1540-1283(I)" template

< class _RI> std :: operator ==(const reverse_iterator< _RI>&,const

reverse_iterator< _RI>&)"不是一个可行的候选人。

" / usr /.../ include / xutility",第499.14行:1540-1283(I)"模板

< class _E,class _Tr> std :: operator ==(const istreambuf_iterator< _E,_Tr>

&,const istreambuf_iterator< _E,_Tr>&)"不是一个可行的候选人。

" / usr /.../ include / xmemory",第155.14行:1540-1283(I)" template< class

_Ty,class _U> std :: operator ==(const allocator< _Ty>&,const

allocator< _U>&)"不是一个可行的候选人。

" / usr /.../ include / string",101.14:1540-1283(I)" template< class

_E,class _Tr,class _A> std :: operator ==(const basic_string< _E,_Tr,_A>

&,const basic_string< _E,_Tr,_A>&)"不是一个可行的候选人。

" / usr /.../ include / string",line 105.14:1540-1283(I)" template< class

_E,class _Tr,class _A> std :: operator ==(const _E *,const

basic_string< _E,_Tr,_A>&)"不是一个可行的候选人。

" / usr /.../ include / string",109.14:1540-1283(I)" template< class

_E,class _Tr,class _A> std :: operator ==(const basic_string< _E,_Tr,_A>

&,const _E *)"不是一个可行的候选人。

" / usr /.../ include / iterator",第184.14行:1540-1283(I)" template

< class _Ty,class _E,class _Tr,class _Dist> std :: operator ==(const

istream_iterator< _Ty,_E,_Tr,_Dist>&,const

istream_iterator< _Ty,_E,_Tr,_Dist>& )"不是一个可行的候选人。

" / usr /.../ include / algorithm",第241.14行:1540-0700(I)之前的

消息已生成处理时

" std :: replace< std :: _ Ptrit< char,long,char *,char&,char *,char

&>, std :: basic_string< char,std :: char_traits< char> ;,s td :: allocator< char> >(_ Ptrit< char,long,char *,char&,char *,char&>,_ Ptrit< char,long,char *,char&,char *,char&>,const basic_string< char,std :: char_traits< char> ;,std :: allo cator< char>>&,const basic_string< char,std :: char_traits< char>,std :: allo cator< char>>&)" ;。



" strtst.cpp",第19.9行:1540-0700(I)上一条消息是

,在处理gsub(const)时产生string&,const string&,const

string&)" ;.

" / usr /.../ include / algorithm",244.29行: 1540-0218(S)电话确实

与operator =的任何参数列表都不匹配。

" / usr /.../ include / algorithm" ;,第244.29:1540-1283行(I)builtin

operator =(char,char)"不是一个可行的候选人。


问候,


Michael


< blockquote> mi ************* @ yahoo.com 写道:

Andrew Koenig写道:

" M" < wh*@somewhere.com>在消息中写道
新闻:TD ****************** @ news-server.bigpond.net.au ...


< ; SNIP>

除非您将对std :: replace的调用更改为使用sNew而不是sData。



您好Andrew,

没有,我仍然是同样的问题(抱歉上面虽然,只是改变了一些变量名称)。

这是测试代码:

#include< iostream>
#include< algorithm>
#include< string>

使用命名空间std;

string gsub (const string& sData,
const string& sFrom,
const string& sTo)
{
string sNew = sData;
if(!sNew。 empty())
{
std :: replace(sNew.begin(),sNew.end(),
sFrom,
sTo);
}
return sNew;
}

int main(void)
{
const string sData =" So me text" ;;
string sFrom =" text";
string sTo =" information";

cout<< 原文:"
<< sData
<< " \ n"
<< 搜索和替换后:
<< gsub(sData,sFrom,sTo)
<< endl;

返回0;
}

以上编译器错误是在linux上产生的,以下错误就在
aix

" / usr /.../ include / algorithm",第243.25行:1540-0218(S)调用
与operator ==的任何参数列表都不匹配。
" / usr /.../ include / utility",第77.14行:1540-1283(I)" template< class
_T1,class _T2> std :: operator ==(const pair< _T1,_T2>&,const
pair< _T1,_T2>&)"不是一个可行的候选人。
" / usr /.../ include / xutility",第384.14行:1540-1283(I)" template
< class _RI> std :: operator ==(const reverse_iterator< _RI>&,const
reverse_iterator< _RI>&)"不是一个可行的候选人。
" / usr /.../ include / xutility",第499.14行:1540-1283(I)" template
< class _E,class _Tr> std :: operator ==(const istreambuf_iterator< _E,_Tr>
&,const istreambuf_iterator< _E,_Tr>&)"不是一个可行的候选人。
" / usr /.../ include / xmemory",第155.14行:1540-1283(I)" template< class
_Ty,class _U> std :: operator ==(const allocator< _Ty>&,const
allocator< _U>&)"不是一个可行的候选人。
" / usr /.../ include / string",101.14:1540-1283(I)" template< class
_E,class _Tr,class _A> ; std :: operator ==(const basic_string< _E,_Tr,_A>
&,const basic_string< _E,_Tr,_A>&)"不是一个可行的候选人。
" / usr /.../ include / string",第105.14行:1540-1283(I)" template< class
_E,class _Tr,class _A> ; std :: operator ==(const _E *,const
basic_string< _E,_Tr,_A>&)"不是一个可行的候选人。
" / usr /.../ include / string",109.14:1540-1283(I)" template< class
_E,class _Tr,class _A> ; std :: operator ==(const basic_string< _E,_Tr,_A>
&,const _E *)"不是一个可行的候选人。
" / usr /.../ include / iterator",第184.14行:1540-1283(I)"模板
<类_Ty,类_E,类_Tr ,类_Dist> std :: operator ==(const
istream_iterator< _Ty,_E,_Tr,_Dist>&,const
istream_iterator< _Ty,_E,_Tr,_Dist>&)"不是一个可行的候选人。
" / usr /.../ include / algorithm",第241.14行:1540-0700(I)之前的
消息是在处理时产生的。
std :: replace< std :: _ Ptrit< char,long,char *,char&,char *,char
&> ;,std :: basic_string< char,std :: char_traits< char>,s TD ::分配器<炭> >

(_ Ptrit< char,long,char *,char&,char *,char&> ;, _Ptrit< char,long,char *,char&,char *,char &>,const basic_string< char,std :: char_traits< char>,std :: allo cator< char>>&,const basic_string< char,std :: char_traits< char> ;,std :: allo cator< char>>&)"。


" strtst.cpp",第19.9行:1540-0700(I)处理gsub(const)时生成上一条消息string&,const string&,const
string&)"。
" / usr /.../ include / algorithm",line 244.29:1540-0218(S)电话
不符合operator =的任何参数列表。
" / usr /.../ include / algorithm",第244.29行:1540-1283(I)builtin
operator =(char,char)"不是一个可行的候选人。

问候,

Michael




如果我没弄错,请更换将替换所有出现的char

与另一个char(例如将所有''a''改为''b''),但不会取代

multi-char子串。


拉里


Hi,

I''ve searched through the previous posts and there seems to be a few
examples of search and replacing all occurrances of a string with
another string.

I would have thought that the code below would work...

string gsub(const string & sData,
const string & sFrom,
const string & sTo)
{
string sNew = sData;

if (! sNew.empty())
{
std::replace(sData.begin(), sData.end(),
sFrom,
sTo);

}
return sNew;
}

But, it produces the following compiler error:

/usr/include/c++/3.2/bits/stl_algo.h: In function `void
std::replace(_ForwardIter, _ForwardIter, const _Tp&, const _Tp&) [with
_ForwardIter = __gnu_cxx::__normal_iterator<const char*,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >

, _Tp = std::basic_string<char, std::char_traits<char>,
std::allocator<char> >]'':


str_tst.cpp:132: instantiated from here
/usr/include/c++/3.2/bits/stl_algo.h:809: no match for `const char& ==
const std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&'' operator
str_tst.cpp:132: instantiated from here
/usr/include/c++/3.2/bits/stl_algo.h:810: assignment of read-only
location
/usr/include/c++/3.2/bits/stl_algo.h:810: cannot convert `const
std::basic_string<char, std::char_traits<char>, std::allocator<char> >''
to `const char'' in assignment

What am I doing wrong here? Looking at the function signature, it should
work. Cannot see why the attempted conversion.

Regards,

Michael

解决方案

"M" <wh*@somewhere.com> wrote in message
news:TD******************@news-server.bigpond.net.au...

I would have thought that the code below would work...

string gsub(const string & sData,
const string & sFrom,
const string & sTo)
{
string sNew = sData;

if (! sNew.empty())
{
std::replace(sData.begin(), sData.end(),
sFrom,
sTo);

}
return sNew;
}



Not unless you change the call to std::replace to use sNew instead of sData.



Andrew Koenig wrote:

"M" <wh*@somewhere.com> wrote in message
news:TD******************@news-server.bigpond.net.au... <SNIP> Not unless you change the call to std::replace to use sNew instead of sData.
Hi Andrew,

No go, I still the same problem (sorry about the above though, just
changed some of the variable names).

Here is the test code:

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

string gsub(const string & sData,
const string & sFrom,
const string & sTo)
{
string sNew = sData;
if (! sNew.empty())
{
std::replace(sNew.begin(), sNew.end(),
sFrom,
sTo);
}
return sNew;
}
int main(void)
{
const string sData = "Some text";
string sFrom = "text";
string sTo = "information";

cout << "Original text: "
<< sData
<< "\n"
<< "After search and replace: "
<< gsub(sData, sFrom, sTo)
<< endl;

return 0;
}
The above compiler error was produced on linux, the following error on
aix

"/usr/.../include/algorithm", line 243.25: 1540-0218 (S) The call does
not match any parameter list for "operator==".
"/usr/.../include/utility", line 77.14: 1540-1283 (I) "template <class
_T1, class _T2> std::operator==(const pair<_T1,_T2> &, const
pair<_T1,_T2> &)" is not a viable candidate.
"/usr/.../include/xutility", line 384.14: 1540-1283 (I) "template
<class _RI> std::operator==(const reverse_iterator<_RI> &, const
reverse_iterator<_RI> &)" is not a viable candidate.
"/usr/.../include/xutility", line 499.14: 1540-1283 (I) "template
<class _E, class _Tr> std::operator==(const istreambuf_iterator<_E,_Tr>
&, const istreambuf_iterator<_E,_Tr> &)" is not a viable candidate.
"/usr/.../include/xmemory", line 155.14: 1540-1283 (I) "template <class
_Ty, class _U> std::operator==(const allocator<_Ty> &, const
allocator<_U> &)" is not a viable candidate.
"/usr/.../include/string", line 101.14: 1540-1283 (I) "template <class
_E, class _Tr, class _A> std::operator==(const basic_string<_E,_Tr,_A>
&, const basic_string<_E,_Tr,_A> &)" is not a viable candidate.
"/usr/.../include/string", line 105.14: 1540-1283 (I) "template <class
_E, class _Tr, class _A> std::operator==(const _E *, const
basic_string<_E,_Tr,_A> &)" is not a viable candidate.
"/usr/.../include/string", line 109.14: 1540-1283 (I) "template <class
_E, class _Tr, class _A> std::operator==(const basic_string<_E,_Tr,_A>
&, const _E *)" is not a viable candidate.
"/usr/.../include/iterator", line 184.14: 1540-1283 (I) "template
<class _Ty, class _E, class _Tr, class _Dist> std::operator==(const
istream_iterator<_Ty,_E,_Tr,_Dist> &, const
istream_iterator<_Ty,_E,_Tr,_Dist> &)" is not a viable candidate.
"/usr/.../include/algorithm", line 241.14: 1540-0700 (I) The previous
message was produced while processing
"std::replace<std::_Ptrit<char,long,char *,char &,char *,char
&>,std::basic_string<char,std::char_traits<char>,s td::allocator<char> >(_Ptrit<char,long,char *,char &,char *,char &>, _Ptrit<char,long,char *,char &,char *,char &>, const basic_string<char,std::char_traits<char>,std::allo cator<char> > &, const basic_string<char,std::char_traits<char>,std::allo cator<char> > &)".


"strtst.cpp", line 19.9: 1540-0700 (I) The previous message was
produced while processing "gsub(const string &, const string &, const
string &)".
"/usr/.../include/algorithm", line 244.29: 1540-0218 (S) The call does
not match any parameter list for "operator=".
"/usr/.../include/algorithm", line 244.29: 1540-1283 (I) "builtin
operator=(char, char)" is not a viable candidate.

Regards,

Michael


mi*************@yahoo.com wrote:

Andrew Koenig wrote:

"M" <wh*@somewhere.com> wrote in message
news:TD******************@news-server.bigpond.net.au...


<SNIP>

Not unless you change the call to std::replace to use sNew instead of sData.



Hi Andrew,

No go, I still the same problem (sorry about the above though, just
changed some of the variable names).

Here is the test code:

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

string gsub(const string & sData,
const string & sFrom,
const string & sTo)
{
string sNew = sData;
if (! sNew.empty())
{
std::replace(sNew.begin(), sNew.end(),
sFrom,
sTo);
}
return sNew;
}
int main(void)
{
const string sData = "Some text";
string sFrom = "text";
string sTo = "information";

cout << "Original text: "
<< sData
<< "\n"
<< "After search and replace: "
<< gsub(sData, sFrom, sTo)
<< endl;

return 0;
}
The above compiler error was produced on linux, the following error on
aix

"/usr/.../include/algorithm", line 243.25: 1540-0218 (S) The call does
not match any parameter list for "operator==".
"/usr/.../include/utility", line 77.14: 1540-1283 (I) "template <class
_T1, class _T2> std::operator==(const pair<_T1,_T2> &, const
pair<_T1,_T2> &)" is not a viable candidate.
"/usr/.../include/xutility", line 384.14: 1540-1283 (I) "template
<class _RI> std::operator==(const reverse_iterator<_RI> &, const
reverse_iterator<_RI> &)" is not a viable candidate.
"/usr/.../include/xutility", line 499.14: 1540-1283 (I) "template
<class _E, class _Tr> std::operator==(const istreambuf_iterator<_E,_Tr>
&, const istreambuf_iterator<_E,_Tr> &)" is not a viable candidate.
"/usr/.../include/xmemory", line 155.14: 1540-1283 (I) "template <class
_Ty, class _U> std::operator==(const allocator<_Ty> &, const
allocator<_U> &)" is not a viable candidate.
"/usr/.../include/string", line 101.14: 1540-1283 (I) "template <class
_E, class _Tr, class _A> std::operator==(const basic_string<_E,_Tr,_A>
&, const basic_string<_E,_Tr,_A> &)" is not a viable candidate.
"/usr/.../include/string", line 105.14: 1540-1283 (I) "template <class
_E, class _Tr, class _A> std::operator==(const _E *, const
basic_string<_E,_Tr,_A> &)" is not a viable candidate.
"/usr/.../include/string", line 109.14: 1540-1283 (I) "template <class
_E, class _Tr, class _A> std::operator==(const basic_string<_E,_Tr,_A>
&, const _E *)" is not a viable candidate.
"/usr/.../include/iterator", line 184.14: 1540-1283 (I) "template
<class _Ty, class _E, class _Tr, class _Dist> std::operator==(const
istream_iterator<_Ty,_E,_Tr,_Dist> &, const
istream_iterator<_Ty,_E,_Tr,_Dist> &)" is not a viable candidate.
"/usr/.../include/algorithm", line 241.14: 1540-0700 (I) The previous
message was produced while processing
"std::replace<std::_Ptrit<char,long,char *,char &,char *,char
&>,std::basic_string<char,std::char_traits<char>,s td::allocator<char> >

(_Ptrit<char,long,char *,char &,char *,char &>, _Ptrit<char,long,char *,char &,char *,char &>, const basic_string<char,std::char_traits<char>,std::allo cator<char> > &, const basic_string<char,std::char_traits<char>,std::allo cator<char> > &)".


"strtst.cpp", line 19.9: 1540-0700 (I) The previous message was
produced while processing "gsub(const string &, const string &, const
string &)".
"/usr/.../include/algorithm", line 244.29: 1540-0218 (S) The call does
not match any parameter list for "operator=".
"/usr/.../include/algorithm", line 244.29: 1540-1283 (I) "builtin
operator=(char, char)" is not a viable candidate.

Regards,

Michael



If I''m not mistaken, replace will replace all occurences of a char
with another char (e.g. change all ''a'' to ''b''), but does not replace
multi-char substrings.

Larry


这篇关于搜索和替换字符串的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆