I have this function to double a string. It puts a string into a stringstream and uses seekp to replace the old value with new value.
Code:
std::string double_str(const std::string & input, std::streamoff offset){
std::stringstream ss(std::ios::in | std::ios::out);
ss.str(input);
ss.seekg(offset, std::ios::end);
int val;
ss >> val;
val *= 2;
ss.seekp(offset,std::ios::end);
ss << val;
return ss.str();
}
When the input string doesn't have the trailing space, seekp doesn't put the new val back into the stream.
Code:
std::cout << double_str("234",-3) << '\n'; // return 234
std::cout << double_str("234 ",-4) << '\n'; // return 468
What did i do wrong that operator<< fails?