C++ std::move和std::copy是否相同?

C++ std::move和std::copy是否相同?,c++,c++11,move-semantics,C++,C++11,Move Semantics,我试着做一些类似的事情: std::copy(std::make_move_iterator(s1.begin()), std::make_move_iterator(s1.end()), std::make_move_iterator(s2.begin())); 得到了这个错误: error: using xvalue (rvalue reference) as lvalue *__result = std::move(*__first); 这让我很困

我试着做一些类似的事情:

std::copy(std::make_move_iterator(s1.begin()), std::make_move_iterator(s1.end()), 
          std::make_move_iterator(s2.begin()));
得到了这个错误:

error: using xvalue (rvalue reference) as lvalue
        *__result = std::move(*__first);
这让我很困惑。如果使用
std::move
,也会发生同样的情况。似乎GCC在内部使用了一个名为
std::uu copy\u move\u a
的函数,该函数移动而不是复制。您使用的是
std::copy
还是
std::move


#包括
#包括
#包括
#包括
#包括
结构测试
{
typedef std::string::value_type value_type;
std::字符串数据;
测试()
{
}
测试(常量字符*数据)
:数据(数据)
{
}
~Test()
{
}
测试(常数测试和其他)
:数据(其他.数据)
{

std::cout
std::move
尽可能移动元素,否则复制。
std::copy
将始终复制

libstdc++的
copy\u move\u a
还接受一个模板参数
\u IsMove
。该参数和迭代器类型一起,它委托给一个
\u copy\u move
类模板,该类模板部分专用于不同的迭代器类别,等等。但最重要的是:是否
移动
。 其中一个专业是

#if __cplusplus >= 201103L
  template<typename _Category>
    struct __copy_move<true, false, _Category>
    // first specialized template argument is whether to move
    {
      template<typename _II, typename _OI>
        static _OI
        __copy_m(_II __first, _II __last, _OI __result)
        {
      for (; __first != __last; ++__result, ++__first)
        *__result = std::move(*__first); // That may be your line
      return __result;
    }
    };
#endif
std::move
隐式包含在
*\u结果中,并且属于相同的值类别,即xvalue

以你为例,

std::copy(std::make_move_iterator(s1.begin()), std::make_move_iterator(s1.end()),
          s2.begin());
应该可以。std::move(a,b,c);
在语义上与

std::copy(std::make_move_iterator(a),
          std::make_move_iterator(b),
          c);
您使用它们的努力都失败了,因为第三个参数-输出迭代器-不应该是移动迭代器。您正在存储到第三个迭代器中,而不是从第三个迭代器中移动。两者都是

std::copy(std::make_move_iterator(s1.begin()),
          std::make_move_iterator(s1.end()),
          s2.begin());


应该做你想做的。

为什么要从
s2.begin()创建移动迭代器
std::copy(std::make_move_iterator(a),
          std::make_move_iterator(b),
          c);
std::copy(std::make_move_iterator(s1.begin()),
          std::make_move_iterator(s1.end()),
          s2.begin());
std::move(s1.begin(), s1.end(), s2.begin());