C :在字符串#附录中移动语义
C++: Move semantic in string#append
我需要将一些字符串合并为一个字符串,出于有效的原因,我想在这种情况下使用移动语义(当然,这些字符串将不再使用)。所以我尝试了
#include <iostream>
#include <algorithm>
#include <string>
int main()
{
std::string hello("Hello, ");
std::string world("World!");
hello.append(std::move(world));
std::cout << hello << std::endl;
std::cout << world << std::endl;
return 0;
}
我认为它将输出
Hello, World!
## NOTHING ##
但实际上输出了
Hello, World!
World!
如果用operator+=
替换append
,它将导致同一件事。做什么的正确方法是什么?
我在Debian 6.10
您不能将string
移至另一个string
的 part 中。这将要求新的string
有效地具有两个存储缓冲区:当前的缓冲区和新的存储缓冲区。然后它必须神奇地使所有连续,因为C 11需要std::string
在连续内存中。
简而言之,您不能。
相关文章:
- 何时在引用或唯一指针上使用移动语义
- 如何从具有移动语义的类对象中生成共享指针
- 可以使用移动语义更改或改进此C++代码吗?
- c++在使用指针时移动语义
- 移动语义和深层/浅层复制之间有什么关系?
- std::unique_lock移动语义
- 移动语义和运算符 + 重载
- C++ 移动语义是否在任何情况下都能节省资源?
- 移动语义在这里如何工作?
- 使用移动语义:右值引用作为方法参数
- 在C++中使用移动语义的正确方法是什么?
- 移动语义 c++ 单链表
- C++:使用整数移动语义
- 当变量和参数名称匹配时,移动语义构造失败
- 在 C++11 中移动语义
- 方法冗余移动调用的移动语义
- 复制省略并在返回值中移动语义
- std::元组和移动语义
- 移动语义与返回shared_ptr?
- C++具有移动语义的可变参数工厂会导致运行时崩溃