添加 std: : string

最有效的预防 std::string的方法是什么?这样做是否值得写出整个函数,或者只需要1-2行?我没看到任何与 std::string::push_front有关的东西。

119559 次浏览

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.

If you're using std::string::append, you should realize the following is equivalent:

std::string lhs1 = "hello ";
std::string lhs2 = "hello ";
std::string rhs = "world!";


lhs1.append(rhs);
lhs2 += rhs; // equivalent to above
// Also the same:
// lhs2 = lhs2 + rhs;

Similarly, a "prepend" would be equivalent to the following:

std::string result = "world";
result = "hello " + result;
// If prepend existed, this would be equivalent to
// result.prepend("hello");

You should note that it's rather inefficient to do the above though.

There actually is a similar function to the non-existing std::string::push_front, see the below example.


Documentation of std::string::insert

#include <iostream>
#include <string>


int
main (int argc, char *argv[])
{
std::string s1 (" world");
std::string s2 ("ello");


s1.insert (0,     s2); // insert the contents of s2 at offset 0 in s1
s1.insert (0, 1, 'h'); // insert one (1) 'h'        at offset 0 in s1


std::cout << s1 << std::endl;
}

output:

hello world

Since prepending a string with data might require both reallocation and copy/move of existing data you can get some performance benefits by getting rid of the reallocation part by using std::string::reserve (to allocate more memory before hand).

The copy/move of data is sadly quite inevitable, unless you define your own custom made class that acts like std::string that allocates a large buffer and places the first content in the center of this memory buffer.

Then you can both prepend and append data without reallocation and moving data, if the buffer is large enough that is. Copying from source to destination is still, obviously, required though.


If you have a buffer in which you know you will prepend data more often than you append a good alternative is to store the string backwards, and reversing it when needed (if that is more rare).

myString.insert(0, otherString);

Let the Standard Template Library writers worry about efficiency; make use of all their hours of work rather than re-programming the wheel.

This way does both of those.

As long as the STL implementation you are using was thought through you'll have efficient code. If you're using a badly written STL, you have bigger problems anyway :)

The problem is efficiency: inserting to the beginning of the string is more expensive as it requires both reallocation and shifting of existing characters.

If you are only prepending to the string, the most efficient way is appending, and then either reverse the string, or even better, go through the string in reverse order.

string s;
for (auto c: "foobar") {
s.push_back(c);
}
for (auto it=s.rbegin(); it!=s.rend(); it++) {
// do something
}

If you need a mix of prepending and appending, I'd suggest using a deque, and then construct a string from it. The double-ended queue supports O(1) insertion and deletion at the beginning and end.

deque<char> dq;
dq.push_front('f');
dq.push_back('o');
dq.push_front('o');
string s {dq.begin(), dq.end()};