Prepend std::string

后端 未结 4 503
醉梦人生
醉梦人生 2021-02-06 20:03

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

4条回答
  •  醉话见心
    2021-02-06 20:46

    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.

提交回复
热议问题