简体   繁体   English

如何直接从 char* 数组创建 std::string 而无需复制?

[英]How to create a std::string directly from a char* array without copying?

Say I have an array of chars, which I have allocated on the heap, and which I want to convert into an std::string.假设我有一个字符数组,我在堆上分配了它,并且我想将其转换为 std::string。 Currently I am doing the following:目前我正在做以下事情:

char *array = new char[size];
WriteIntoArray(array, size);
std::string mystring(array);
delete[] array;
return mystring; // or whatever

From what I read on the internet ( http://www.cplusplus.com/reference/string/string/string/ ), the string constructor performs a copy of the buffer I pass it, leaving me to free the buffer (later, the string frees its internal buffer).根据我在互联网上阅读的内容( http://www.cplusplus.com/reference/string/string/string/ ),字符串构造函数执行我传递的缓冲区的副本,让我释放缓冲区(稍后,字符串释放其内部缓冲区)。 What I would like to do is to allocate my buffer, transfer control of it to the string, and then have it free my buffer when it is destructed.我想做的是分配我的缓冲区,将它的控制权转移到字符串,然后让它在我的缓冲区被破坏时释放它。

The question initializing std::string from char* without copy looked promising, but my code relies on API calls ("WriteIntoArray" in the above example) which have to write into an array of chars, so I have to create a C-style char* buffer, and cannot convert my code to use only built-in string operations (which was the answer suggested). 从 char* 初始化 std::string 而没有复制的问题看起来很有希望,但是我的代码依赖于 API 调用(上例中的“WriteIntoArray”),它必须写入一个字符数组,所以我必须创建一个 C 风格char* 缓冲区,并且无法将我的代码转换为仅使用内置字符串操作(这是建议的答案)。

Is there some standard way to do this, or should I just write my own string class (ugh)?有没有一些标准的方法来做到这一点,或者我应该写我自己的字符串 class (呃)? Thanks!谢谢!

If you're using a conforming C++0x implementation, or one of the many C++ libraries that guarantee contiguous storage for std::string , then you can get a char* that points directly to the storage used by your std::string .如果您使用的是符合标准的 C++0x 实现,或者是保证std::string连续存储的众多 C++ 库之一,那么您可以获得一个直接指向std::string使用的存储的char* .

For example:例如:

std::string mystring;
mystring.resize(size);
WriteIntoArray(&mystring[0], size);
return mystring;

You may have to think carefully about a null terminator, however.但是,您可能需要仔细考虑 null 终结器。

You may implement your own std::allocator that instead of allocating new memory for your string, uses as the source the region you already have instantiated.您可以实现自己的 std::allocator ,而不是为您的字符串分配新的 memory ,而是使用您已经实例化的区域作为源。

You should then instantiate your std::string using your allocator.然后,您应该使用分配器实例化您的 std::string。

http://www.codeproject.com/Articles/4795/C-Standard-Allocator-An-Introduction-and-Implement http://www.codeproject.com/Articles/4795/C-Standard-Allocator-An-Introduction-and-Implement

You can't std::string owns the pointer and thus copies the data into a space that it allocates.您不能 std::string 拥有指针,从而将数据复制到它分配的空间中。

What you could do is:你可以做的是:

std::string  mystring(size, ' ');
WriteIntoArray(&mystring[0], size);
return mystring; // or whatever

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM