There is a function

void TrimLeft(std::string& str, const char* chars2remove) { if (!str.empty()) { std::string::size_type pos = str.find_first_not_of(chars2remove); if (pos != std::string::npos) str.erase(0,pos); else str.erase( str.begin() , str.end() ); // make empty } } 

In principle, it is clear that it cuts off the chars2remove characters from the left. If you call her

 TrimLeft(s, " "); 

then everything works fine. But I need to check not only for the space, but also for other non-printing characters, and then I call the function like this:

 char *cc = new char[7]; cc[0]= ' '; cc[1] = (char)8; cc[2] = (char)9; cc[3] = (char)10; cc[4] = (char)11; cc[5] = (char)12; cc[6] = (char)13; TrimLeft(s, сс); 

In this case, when s = "alder" or s = "aspen" (the first letter in the line o), then pos = 1 and the first letter is cut off. I can not understand what's wrong.

    1 answer 1

    Well, everything is logical. What does the function want? pointer to an array of characters. Ok, he was given it. But how find_first_not_of function know where the end of this array is? That's right, by zero zero. Where is the null character in your cc array? it is not known where and there is. And what will be there, until it is found - is unknown. If you do not believe, insert after cc[6] = (char)13; call strlen (cc) and see the size of the string. He will definitely not be 7.

    What to do? Write correctly!

     char *cc = new char[8]; cc[0]= ' '; cc[1] = (char)8; cc[2] = (char)9; cc[3] = (char)10; cc[4] = (char)11; cc[5] = (char)12; cc[6] = (char)13; cc[7] = (char)0; TrimLeft(s, сс); 

    Of course, it is better to create a static array in memory, rather than creating it every time.

    • thank you very much. I have two heads) and there was a thought, but something did not linger on the array - I agree) - torymo