3

I am still new to STL and wanted to replace all occurrences of ch in a string with k.

I tried the following:

std::replace (str.begin(), str.end(), "ch", "k");

But it threw this error:

no matching function for call to ‘replace(__gnu_cxx::__normal_iterator<char*,
  std::basic_string<char, std::char_traits<char>, std::allocator<char> > >,
  __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>,
  std::allocator<char> > >, const char [2], const char [1])

How do I get replace to work in this case?

Note: I saw a similar question but in that case the OP was using "blah" and 'b' as arguments to be replaced but here both of my arguments are strings.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
ofey
  • 423
  • 1
  • 7
  • 17

3 Answers3

7

Since the definition of std::replace is

template< class It, class T >
void replace( It first, It last, const T& old_value, const T& new_value );

Reference http://en.cppreference.com/w/cpp/algorithm/replace

You must pass a char because a std::string is std::basic_string<char> and T is a char.

For example:

std::replace (str.begin(), str.end(), 'c',  'k');

To solve your problem, read: How do I replace all instances of a string with another string?

Community
  • 1
  • 1
masoud
  • 55,379
  • 16
  • 141
  • 208
3

The C++ Standard Library does not contain a replace function like the one you're looking for here. Replace is a general algorithm that replaces all occurrences of one element in any sequence (be that sequence one of chars, ints, or YourTypes) with some other element value. It does not, for instance, have the ability to change the length of the string. (How could it? Changing the size of the string requires calling member functions of string, and replace doesn't have a reference to the string)

If you need this kind of replace you probably want to consider the Boost String Algorithms library.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • Just as an aside, I can't use boost library for things like TopCoder, right? – ofey May 26 '13 at 18:49
  • @ofey: I don't know what TopCoder is. – Billy ONeal May 26 '13 at 18:49
  • I meant for real time online algorithmic coding competitions. I would have to use whatever libraries they support, right? I mean simply adding a header like will not work? Or would it? – ofey May 26 '13 at 18:51
  • @ofey: Oh, probably not then. Unless of course they have a boost installation installed already. – Billy ONeal May 26 '13 at 19:21
2

The error message is quite clear : you need to use char for the values, not c-strings. That means, it is not possible to replace 2 characters with one.


If you want to replace sub-strings of a string with another string, you can use this solution :

void replaceAll(std::string& str, const std::string& from, const std::string& to) {
    if(from.empty())
        return;
    std::string::size_type start_pos = 0;
    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
    }
}
Community
  • 1
  • 1
BЈовић
  • 62,405
  • 41
  • 173
  • 273