是否有可能从std::string中获取内存(就像string move tor所做的那样)
Is it possible to take memory from std::string(like string move ctor does)?
如果我有我自己的vector<char>
版本的内部类(我控制源代码),并且为了示例,我不能将其更改为std::string
,是否有方法从std::string
窃取内存,就像移动std::string
的构造函数一样。
所以像这样:
std::string str{"abcdefghijklmnopqrstu"};
MyVectorCharClass mvc(std::move(str)); // Constructor takes memory from str
我想我听说过一些未来的建议,将.release()
添加到std::string
或std::vector
中,但我说的是现在。
No。std::string
管理的缓冲区是私有的。您可以通过&str[0]
访问它,但string
仍然拥有它,并在它超出范围时将其销毁。您无法告诉str
它现在拥有一个不同的缓冲区,或者将其底层缓冲区设置为nullptr
,或者以任何其他方式使其而不是 delete
该缓冲区。
那是std::string
的工作。它拥有它的缓冲区,它不会放弃它。
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- std::string 的对象真的可以移动吗?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- 我叫删除,但我的d-tor没有叫,为什么?
- 为什么 std::runtime_error 的 c'tor 经常引用 std::string?
- 是否有可能从std::string中获取内存(就像string move tor所做的那样)