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.

3
  • 4
    The last two arguments must be of type char in this case... Commented May 26, 2013 at 18:29
  • Consider Boost's replace_all function. It works with replacing substrings with other strings if memory serves. Commented May 26, 2013 at 18:34
  • @OliCharlesworth technically, the last two arguments need to be of a type that is both comparable to, and assignable to, char. Commented May 26, 2013 at 18:46

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

Just as an aside, I can't use boost library for things like TopCoder, right?
@ofey: I don't know what TopCoder is.
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 <boost/algorithm/string_regex.hpp> will not work? Or would it?
@ofey: Oh, probably not then. Unless of course they have a boost installation installed already.
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'
    }
}

5 Comments

i can't use different sized strings or I cant use strings at all? Could I do something like replace all "ch" with "k%" and then delete all the '%' later?
+1 for the code snippet. Of course, someone should technically use std::string::size_type rather than size_t, but that's a nitpick.
@BillyONeal Or auto in while
@BJ: No, auto would do the wrong thing if the needle contained the replacement.
"The error message is quite clear". Maybe to some people. For everyone else that's kinda condescending.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.