What is the most efficient way to prepend std::string
? Is it worth writing out an entire function to do so, or would it take only 1 - 2 lines? I\'m not seeing anyth
There is an overloaded string operator+ (char lhs, const string& rhs);
, so you can just do your_string 'a' + your_string
to mimic push_front
.
This is not in-place but creates a new string, so don't expect it to be efficient, though. For a (probably) more efficient solution, use resize
to gather space, std::copy_backward
to shift the entire string back by one and insert the new character at the beginning.