It is necessary to read the file and replace all Cyrillic characters with spaces, then display the result. What do I need to pass as the third parameter replace_copy_if?

{ ifstream in; in.open(fileName, ios::in); if (in.is_open()) { setlocale(LC_ALL, "Russian"); string str((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); string str1; replace_copy_if( str.begin(), str.end(), str1.begin(), [](char n) {return ( n > -65 && n < 0); }, ' ' ); ostream_iterator<char> out(cout); copy(str1.begin(), str1.end(), out); } else { cout << "Could not open the file" << endl; } } 

    2 answers 2

    The third parameter must be an iterator at the beginning of the range in which the result will be written. Moreover, the size of this range should not be less than the original. This condition is not met in your code. I see 3 options for how to fix it.

    1) Increase the size of str1 to the size of str with the filling constructor :

     string str1(str.size(), 0); 

    2) Use std :: back_inserter :

     std::replace_copy_if( str.begin(), str.end(), std::back_inserter(str1), [](char n) {return ( n > -65 && n < 0); }, ' '); 

    3) Display directly on the screen:

     std::replace_copy_if( str.begin(), str.end(), std::ostream_iterator<char>(std::cout), [](char n) {return ( n > -65 && n < 0); }, ' '); 

      There must be a unary predicate, that is, a function that returns true or false. In your case, it should be something like "Cyrillic found."

      For such tasks, it is easier to use ordinary string algorithms contained in the string library.

      Full details http://en.cppreference.com/w/cpp/algorithm/replace