Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
For some reason, the replace template from <algorithm> does not want to work for me. See example code below:

C++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	string s = "Hello, world!";
	replace(s.begin(), s.end(), "l", "i");
	cout << s << endl;
	return 0;
}


There are no syntax errors with the code, but the compiler is still throwing me the following errors:

In file included from /usr/include/c++/4.9/algorithm:62:0,
                 from prog.cpp:3:
/usr/include/c++/4.9/bits/stl_algo.h: In instantiation of 'void std::replace(_FIter, _FIter, const _Tp&, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; _Tp = char [2]]':
prog.cpp:8:38:   required from here
/usr/include/c++/4.9/bits/stl_algo.h:4234:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
  if (*__first == __old_value)
               ^
/usr/include/c++/4.9/bits/stl_algo.h:4235:13: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
    *__first = __new_value;
             ^


Thanks, all!

What I have tried:

I have tried multiple versions of this, but nothing seems to work. Google just gives me the template, which I already know.
Posted
Updated 18-Aug-16 21:32pm

1 solution

Your call replace(iterator, iterator, const char*, const char *) does not match the std::replace template:
C++
template<typename _FIter, typename _Tp>
  void
  replace(_FIter, _FIter, const _Tp&, const _Tp&);

_Tp is char here because the std::string iterators are pointing to char.

So you must use
replace(s.begin(), s.end(), 'l', 'i');
 
Share this answer
 
Comments
Anti-Antidote 19-Aug-16 8:28am    
...would I need to use char s rather than string s?
Jochen Arndt 19-Aug-16 8:48am    
No.

You still did not get it. I will try to explain it in a different way:

The replace() template works with an iterator and the corresponding type.
The first thing is to understand what an iterator is. From http://www.cplusplus.com/reference/iterator/:

"An iterator is any object that, pointing to some element in a range of elements (such as an array or a container), has the ability to iterate through the elements of that range using a set of operators (with at least the increment (++) and dereference (*) operators)."

In the case of std::string you have an array of characters. So the type (_Tp) is char.

Or just think about what you get when using the iterator to access the element:

std::string s = "Hello, world!";
cout << "The first char is " << *s.begin() << endl;
Anti-Antidote 19-Aug-16 10:29am    
Using *s.begin() and *s.end() aren't working for me... am I still doing it wrong?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900