M
M
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*,
/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
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*,
str_tst.cpp:132: instantiated from here, _Tp = std::basic_string<char, std::char_traits<char>,
std::allocator<char> >]':
/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