I am writing on the pros in Xcode. To remove spaces in a line I use:

lastZero.erase(remove( lastZero.begin(), lastZero.end(), ::isspace), lastZero.end()); 

Compiling code, launching and executing an application is excellent. I upload the code to the task site (which I am performing), mark "C ++ 11 4.8.2 - GNU C ++ Compiler with options: -lm -lcrypt -O2 -std = c ++ 11 -pipe", the compiler gives an error:

 code.cpp: In function 'int main(int, const char**)': code.cpp:235:71: error: cannot convert 'std::basic_string::iterator {aka __gnu_cxx::__normal_iterator >}' to 'const char*' for argument '1' to 'int remove(const char*)' lastZero.erase( remove( lastZero.begin(), lastZero.end(), ' ' ), lastZero.end() ); 

    1 answer 1

    I see at least one difference: ::isspace in Xcode and ' ' in the gcc error message. Maybe the old source version was copied?

    It is important that ::isspace be used with std::remove_if , and not std::isspace so that the compilation is successful:

     #include <iostream> #include <string> #include <algorithm> // remove_if int main() { std::string string("SPACES"); std::cout << string << std::endl; string.erase(std::remove_if(string.begin(), string.end(), ::isspace), string.end()); std::cout << string << std::endl; } 

    v.erase( std::remove_if(std::begin(v), std::end(v), is_x), std::end(v) ); The idiom to remove items from the sequence works .

    Caution: if the string contains characters outside ascii, then the behavior of ::isspace not defined if the char type has a sign in this implementation .

    • Yes. That I changed to :: isspace. The same mistake. Is there any analogue to remove spaces with a single line of code? - iSerg
    • one
      I just checked the code on ideone, it works with c ++ 11 . Can you add a complete, but minimal example that demonstrates the problem to the question? - jfs
    • 2
      @iSerg: You write remove , but you need remove_if . That is, if isspace , then remove_if , and if ' ' , then remove . - VladD
    • It all worked. Thank. - iSerg