I was implementing a method to remove certain characters from a string txt, in-place. the following is my code. The result is expected as "bdeg". however the result is "bdegfg", which seems the null terminator is not set. the weird thing is that when I use gdb to debug, after setting null terminator
(gdb) p txt $5 = (std::string &) @0xbffff248: {static npos = <optimized out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x804b014 "bdeg"}} it looks right to me. So what is the problem here?
#include <iostream> #include <string> using namespace std; void censorString(string &txt, string rem) { // create look-up table bool lut[256]={false}; for (int i=0; i<rem.size(); i++) { lut[rem[i]] = true; } int i=0; int j=0; // iterate txt to remove chars for (i=0, j=0; i<txt.size(); i++) { if (!lut[txt[i]]){ txt[j]=txt[i]; j++; } } // set null-terminator txt[j]='\0'; } int main(){ string txt="abcdefg"; censorString(txt, "acf"); // expect: "bdeg" std::cout << txt <<endl; } follow-up question:
if string is not truncated like c string. so what happens with txt[j]='\0' and why it is "bdegfg" not 'bdeg'\0'g' or some corrupted strings.
another follow-up: if I use txt.erase(txt.begin()+j, txt.end()); it works fine. so I'd better use string related api. the point is that I do not know the time complexity of the underlying code of these api.
bool lut[256] = {};-- When you provide an initializer for an array, any unspecified elements are value initialized. Forbool, value initialized meansfalse.= {false};will initialize the first element tofalse, and then, because there is an initializer at all, all of the elements will be initialized tofalse. If he had written= {true};, that would initialize the first element totrue, and all the rest tofalse(again, because he provided an initializer). If, however, he had simply writtenbool lut[256];-- with no initializer, the elements would be left uninitialized, in an uncertain state. It's just a rule of the language.