C++ 前置 std::string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8468597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prepend std::string
提问by zeboidlund
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 anything related to an std::string::push_front
.
什么是最有效的前置方式std::string
?是否值得写出整个函数来这样做,还是只需要 1 - 2 行?我没有看到任何与std::string::push_front
.
回答by Filip Roséen - refp
There actually is a similar function to the non-existing std::string::push_front
, see the below example.
实际上有一个与不存在的函数类似的函数std::string::push_front
,请参见下面的示例。
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).
由于在字符串前面加上数据可能需要重新分配和复制/移动现有数据,因此您可以通过使用std::string::reserve
(预先分配更多内存)摆脱重新分配部分来获得一些性能优势。
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.
遗憾的是,数据的复制/移动是不可避免的,除非您定义自己的自定义类,该类的行为类似于std::string
分配一个大缓冲区并将第一个内容放在此内存缓冲区的中心。
Then you can both prepend and append data without reallocation and moving data, if the buffer is large enough that is. Copying from sourceto destinationis still, obviously, required though.
然后,如果缓冲区足够大,您可以在不重新分配和移动数据的情况下预先添加和附加数据。显然,从源复制到目标仍然是必需的。
If you have a buffer in which you know you will prependdata more often than you appenda good alternative is to store the string backwards, and reversing it when needed (if that is more rare).
如果你有,你知道你会缓冲预先考虑往往比你的数据追加一个很好的选择是存储字符串倒退,并在需要时扭转它(如果是较为少见)。
回答by matiu
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 :)
只要您使用的 STL 实现经过深思熟虑,您就会拥有高效的代码。如果您使用的是写得不好的 STL,那么无论如何您都会遇到更大的问题:)
回答by Mike Bailey
If you're using std::string::append
, you should realize the following is equivalent:
如果您正在使用std::string::append
,您应该意识到以下内容是等效的:
std::string lhs1 = "hello ";
std::string lh2 = "hello ";
std::string rhs = "world!";
lhs1.append(rhs);
lhs2 += rhs; // equivalent to above
// Also the same:
// lhs2 = lhs + 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.
您应该注意,虽然执行上述操作效率很低。
回答by Alexander Gessler
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
.
有一个重载string operator+ (char lhs, const string& rhs);
,所以你可以只做your_string 'a' + your_string
模仿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.
这不是就地而是创建一个新字符串,所以不要指望它是有效的。对于(可能)更有效的解决方案,使用resize
来收集空间,std::copy_backward
将整个字符串向后移动一个并在开头插入新字符。