I'd like to initialise a string and then modify the resultant char array. I did:
std::string str = "hello world"; const char* cstr = str.c_str(); // Modify contents of cstr However, because cstr is const, I cannot modify the elements. If I remove const, I cannot use c_str().
What is the best way to achieve this?
std::stringdirectly.const_cast, however, it's a very, very bad idea as internal members in the string assume that only they're modifying the buffer, and depending on how you change it you can get unexpected and strange behavior later. (Don't ask me how I know, I'd rather not think about it again,)