C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ move()

C++ 算法函数move()

C++ 算法move()函数用于移动元素。它接受三个参数,然后将属于[first,last)范围的元素移到以'result'开头的范围。

语法

template<class InputIterator, class OutputIterator> OutputIterator move(InputIterator first, InputIterator last, OutputIterator result);

参数

first: 它是范围的第一个元素的输入迭代器,元素本身包含在范围中。
last: 它是范围的last元素的输入迭代器,其中元素本身不包含在范围中。
result: 它是移动元素初始位置的输出迭代器。

返回值

该函数将第一个元素的迭代器返回到已移动元素的序列。

示例1

#include <iostream>     
#include <algorithm>    
#include <utility>      
#include <vector>       
#include <string>       
int main () 
{
  std::vector<std::string> a = {"suraj","aman","vanshika","chhavi"};
  std::vector<std::string> b (4);
  std::cout << "Move function.\n";
  std::move ( a.begin(), a.begin()+4, b.begin() );
  std::cout << "a contains " << a.size() << " elements:";
  std::cout << " (The state of which is valid.)";
  std::cout << '\n';
  std::cout << "b contains " << b.size() << " elements:";
  for (std::string& x: b) std::cout << " [" << x << "]";
  std::cout << '\n';
  std::cout << "Moving the conatiner a...\n";
  a = std::move (b);
  std::cout << "a contains " << a.size() << " elements:";
  for (std::string& x: a) std::cout << " [" << x << "]";
  std::cout << '\n';
  std::cout << "b is in valid state";
  std::cout << '\n';
  return 0;
}
输出:
Move function.
a contains 4 elements: (The state of which is valid.)
b contains 4 elements: [suraj] [aman] [vanshika] [chhavi]
Moving the conatiner a...
a contains 4 elements: [suraj] [aman] [vanshika] [chhavi]
b is in valid state

示例2

#include<bits/stdc++.h>
int main()
{
  std :: vector <int> u1 {9, 14, 21, 18};
  std :: vector <int> u2 {14, 14, 14, 14};
  std :: cout << "u1 contains :";
  for(int j = 0; j < u1.size(); j++)
    std :: cout << " " << u1[j];
  std :: cout << "\n";
  std :: cout << "u2 contains :";
  for(unsigned int j = 0; j < u2.size(); j++)
    std :: cout << " " << u2[j];
  std :: cout << "\n\n";
  std :: move (u1.begin(), u1.begin() + 4, u2.begin() + 1);
  std :: cout << "u2 contains after move function:";
  for(unsigned int j = 0; j < u2.size(); j++)
    std :: cout << " " << u2[j];
  std :: cout << "\n";
  return 0;
}
输出:
u1 contains : 9 14 21 18
u2 contains : 14 14 14 14
u2 contains after move function: 14 9 14 21

复杂度

该函数的复杂度从第一个元素到last元素都是线性的。

数据竞争

访问某些或所有容器对象。

异常

如果任何容器元素抛出一个异常,该函数将引发异常。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4