c++ - why the result is nothing for this code sample about string toupper? -


this question has answer here:

string str("fujian"); string newstr; transform(str.begin(), str.end(), newstr.begin(), ::toupper); cout << newstr << endl; 

why result nothing code sample string toupper?

your code writes past end of newstr , therefore has undefined behaviour.

try either of following instead:

// version 1 string str("fujian"); string newstr(str); transform(newstr.begin(), newstr.end(), newstr.begin(), ::toupper); cout << newstr << endl;  // version 2 string str("fujian"); string newstr; transform(str.begin(), str.end(), std::back_inserter(newstr), ::toupper); cout << newstr << endl; 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -