Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

move

函数模板  <algorithm>
template <class InputIterator, class OutputIterator>
  OutputIterator move (InputIterator first, InputIterator last, OutputIterator result);

移动范围的元素

将范围 [first,last)中的元素移动到从result开始的范围中。

[first,last)中元素的值被转移到result所指向的元素中。 调用之后,范围[first,last)中的元素将保持未指定但有效的状态。

范围不应重叠,即result指向范围 [first,last)中的一个元素。 对于这种情况,请参见move_backward。

这个函数模板的行为相当于:
template<class InputIterator, class OutputIterator>
  OutputIterator move (InputIterator first, InputIterator last, OutputIterator result)
{
  while (first!=last) {
    *result = std::move(*first);
    ++result; ++first;
  }
  return result;
}

☲  参数


first, last
指向一个将要移动的序列初始和最终位置的输入迭代器。使用的范围是[first,last), 它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
result
指向目标序列中的初始位置的输出迭代器。
这将不会指向范围 [first,last)中的任何元素。

☉  返回值



指向已移动元素的目标范围末端的迭代器。

☣  示例



// move algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::move (ranges)
#include <utility>      // std::move (objects)
#include <vector>       // std::vector
#include <string>       // std::string

int main () {
  std::vector<std::string> foo = {"air","water","fire","earth"};
  std::vector<std::string> bar (4);

  // moving ranges:
  std::cout << "Moving ranges...\n";
  std::move ( foo.begin(), foo.begin()+4, bar.begin() );

  std::cout << "foo contains " << foo.size() << " elements:";
  std::cout << " (each in an unspecified but valid state)";
  std::cout << '\n';

  std::cout << "bar contains " << bar.size() << " elements:";
  for (std::string& x: bar) std::cout << " [" << x << "]";
  std::cout << '\n';

  // moving container:
  std::cout << "Moving container...\n";
  foo = std::move (bar);

  std::cout << "foo contains " << foo.size() << " elements:";
  for (std::string& x: foo) std::cout << " [" << x << "]";
  std::cout << '\n';

  std::cout << "bar is in an unspecified but valid state";
  std::cout << '\n';

  return 0;
}

输出:
Moving ranges...
foo contains 4 elements: (each in an unspecified but valid state)
bar contains 4 elements: [air] [water] [fire] [earth]
Moving container...
foo contains 4 elements: [air] [water] [fire] [earth]
bar is in an unspecified but valid state

✥ 复杂度



first and last之间的距离是线性的:对范围内的每个元素执行移动赋值。

⇄ 数据竞争


修改两个范围内的对象。

☂ 异常安全性



如果元素move-赋值(assignment)或迭代器上的操作抛出,则抛出。
注意,无效的参数会导致未定义的行为。

🍄  另请参阅



copy 复制范围的元素(函数模板)
move_backward 从末尾移动范围的元素(函数模板)

联系我们 免责声明 关于CandCplus 网站地图