I was trying to transform a string into lowercase and store it in another variable using std::transform
and std::tolower
. I first tried:
string str1("Hello");
string lowerStr1;
transform(str1.begin(), str1.end(), lowerStr1.begin(), ::tolower);
cout << lowerStr1 << endl;
But, lowerStr1
contained nothing. After initializing lowerStr1
with str1
, I got the desired result. I want to know the intuition behind this. Could someone explain why lowerStr1
should be initialized in this case?