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
Post a Comment