c++ - move 使用无效?

标签 c++ visual-studio vector move

有人能告诉我为什么这段代码没有输出任何东西吗? 我假设它与 move 线有关...

#include <iostream> 
#include <vector> 
#include <algorithm> 

using namespace std; 

int main(){ 
vector<int> v{66,79,154,24,76,13,7}; 

v = move(v); 
for(auto i: v) 
cout << i << " "; 
}

更新:所以我添加了 system("pause");帮助自己。我是否需要它不是我关注的重点。当我在 Visual Studio 2013 中再次运行代码时,它成功了。但是,当我使用 C++14 通过 Ideone 运行它时,它没有输出任何内容。现在有点困惑。

Visual Studio 2013 Ideone

最佳答案

使用xvalue 参数调用的标准库函数可能假定参数是对对象的唯一引用;如果它是从带有 std::movelvalue 构造的,则不进行别名检查。特别是,这意味着标准库 move 赋值运算符不必执行自赋值检查:

std::vector<int> v = {2, 3, 3};
v = std::move(v); // undefined behavior

更多详情请引用std::movethis question

关于c++ - move 使用无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42188844/

相关文章:

c++ - 访问 vector 中的 map : map<string, vector<map<string, vector<int>>>>

sql-server - 如何创建ssis项目

c# - 如何在不使用类别的情况下在属性网格中添加组?

wpf - 从图像创建矢量

vector - 在 C++ 中执行 k 意味着在二进制向量上进行聚类的快速方法

c++ - 在 Windows 上构建 GnuMP 库 dll 的过程

C++11。 Lambdas成员变量捕获,STL列表中的 "this"指针陷阱

c++ - std::map::operator[] 比 std::map::insert 更有效吗?

asp.net - 在 ASPX 页面的源 View 中复制和粘贴控件 : How to force VS not to touch my IDs?

c++ - 为什么我可以访问我刚刚从 C++ 中的 STL vector 中删除的元素?