I have a line whose last part (suffix) needs to be changed several times, and I need to generate new lines. I am trying to use ostringstream to do this, as I think using streams will be faster than string concatenation. But when the previous suffix is larger than the previous one, it becomes corrupted. The stream also disables null characters.
#include<iostream>
#include<sstream>
using namespace std;
int main()
{
ostringstream os;
streampos pos;
os << "Hello ";
pos = os.tellp();
os << "Universe";
os.seekp(pos);
cout<< os.str() << endl;
os << "World\0";
cout<< os.str().c_str() << endl;
return 0;
}
Output
Hello Universe
Hello Worldrse
But I want to Hello World. How should I do it? Is there any other way to do this faster?
Edit:
Adds std::ends. But I wonder how it works domestically. I would also like to know if there are faster ways to do the same.