move原型:

std::move

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

该函数是将指定范围内的元素移动到从result開始的位置。

move之后。[first,last)范围内的元素去留的详细实现由编译器决定。

result不能是在[first,last)范围内。

返回值为result中最后一个被覆盖元素的下一个位置元素的迭代器

其行为类似于:


3
4
5
6
7
8
9
template<class InputIterator, class OutputIterator>
OutputIterator move (InputIterator first, InputIterator last, OutputIterator result)
{
while (first!=last) {
*result = std::move(*first);
++result; ++first;
}
return result;
}

一个简单的样例:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void mmove(){
vector<int> vi{3,5,1,1};
vector<int> v2{3,5,5,1};
vector<int> result{1,2,3,4,5,6,7};
cout<<"vi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
cout<<"result=";
for(int i:result)
cout<<i<<" ";
cout<<endl;
auto it=move(vi.begin(),vi.end(),result.begin());
cout<<"after auto it=move(vi.begin(),vi.end(),result.begin())"<<endl;
cout<<"vi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
cout<<"result=";
for(int i:result)
cout<<i<<" ";
cout<<endl;
cout<<"it="<<*it<<endl; cout<<"v2=";
for(int i:v2)
cout<<i<<" ";
cout<<endl;
auto it2=move(v2.begin()+1,v2.end(),v2.begin());
cout<<"after auto it2=move(v2.begin()+1,v2.end(),v2.begin());"<<endl;
cout<<"v2=";
for(int i:v2)
cout<<i<<" ";
cout<<endl;
if(it2==v2.end())
cout<<"it2==v2.end()"<<endl;
else
cout<<"it2="<<*it2<<endl; }

执行截图:



能够看到。假设result在[first,last)范围内,将改写原来的元素。

特别是有可能你move的元素是你已经改动了的元素,导致了你不希望的行为。

move_backward原型:

std::move_backward

template <class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 move_backward (BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 result);

该函数是将范围[first,last)内的元素从后往前移动到result的位置。result覆盖的顺序也是逆序的。

该函数返回目的范围result的从顺序来看第一个被覆盖的元素(是指顺着看第一个被覆盖的元素而不是首先被覆盖的元素)(看以下详细的样例)。

其行为类似于:

template<class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 move_backward ( BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 result )
{
while (last!=first) *(--result) = std::move(*(--last));
return result;
}

一个简单的样例:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void mmovebackward(){
vector<int> vi{99,5,1,1};
vector<int> result{1,2,88,4,5,6,7};
cout<<"vi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
cout<<"result=";
for(int i:result)
cout<<i<<" ";
cout<<endl;
auto it=move_backward(vi.begin(),vi.end(),result.end());
cout<<"after auto it=move_backward(vi.begin(),vi.end(),result.end())"<<endl;
cout<<"vi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
cout<<"result=";
for(int i:result)
cout<<i<<" ";
cout<<endl;
cout<<"it="<<*it<<endl; }

执行截图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4NDQzNTIxNTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

注意的是。返回的是指向result中的99元素的迭代器,而不是first,last范围内的first!

——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,能够在以下留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我改动,更好的分享给大家。谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-19

于GDUT

———

STL algorithm算法mov,move_backward(38)的更多相关文章

  1. STL algorithm算法merge(34)

    merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...

  2. STL algorithm算法mismatch(37)

    mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...

  3. STL algorithm算法is_permutation(27)

    is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...

  4. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

  5. STL algorithm算法minmax,minmax_element(36)

    minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T ...

  6. STL algorithm算法min,min_element(35)

    min样板: std::min C++98 C++11 C++14 default (1) template <class T> const T& min (const T& ...

  7. STL algorithm算法max,max_elements(33)

    max原型: std::max C++98 C++11 C++14 default (1) template <class T> const T& max (const T& ...

  8. STL algorithm算法make_heap和sort_heap(32)

    make_heap原型: std::make_heap default (1) template <class RandomAccessIterator> void make_heap ( ...

  9. STL algorithm算法lexicographical_compare(30)

    lexicographical_compare原型: std::lexicographical_compare default (1) template <class InputIterator ...

随机推荐

  1. 在 Android 应用程序中使用 SQLite 数据库以及怎么用

    part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...

  2. sdut 2805(最小生成树)

    大家快来A水题 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 (1<= N <=2000)(1<= M <= N*(N-1)/2 ...

  3. Vue v-if v-for v-bind v-on

    v-if <div v-if="type === 'A'"> A </div> <div v-else-if="type === 'B'&q ...

  4. 使用Vue动态生成form表单

    form-create 表单生成器 具有数据收集.校验和提交功能的表单生成器,支持双向数据绑定和事件扩展,组件包含有复选框.单选框.输入框.下拉选择框等表单元素以及省市区三级联动,时间选择,日期选择, ...

  5. Linux-swap分区

    Linux内核为了提高读写效率与速度,会将文件在内存中进行缓存,这部分内存就是Cache Memory(缓存内存).即使你的程序运行结束后, Cache Memory也不会自动释放.这就会导致你在Li ...

  6. Python day字符串所有使用

    字符串所有的操作name = "dio"names = "my\t name is {Name} and i am a {job}"print(name.cap ...

  7. 今日SGU 5.5

    SGU 114 题意:求一个点到其他点的距离总和最小,距离的定义是x轴距离乘以那个点的人数p 收获:带权中位数,按坐标排序,然后扫一遍,最后权值超过或等于总权值的一半时的那个点就是答案,证明暂无 #i ...

  8. POJ——T 1961 Period

    http://poj.org/problem?id=1961 Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 18542   ...

  9. 洛谷——P1307 数字反转

    https://www.luogu.org/problem/show?pid=1307#sub 题目描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原 ...

  10. 洛谷 P3371 【模板】单源最短路径

    P3371 [模板]单源最短路径 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出 ...