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. NOIP2009 最优贸易(BFS)

    本题正解是tarjan.我没有去写 之前的代码是错误的不好意思,因为数据太弱一直没有发现. 相同还是两遍bfs,一次正向,一次反向.在正向的时候我们求出从起点走到各个点的最小值.在反向的时候求出从终点 ...

  2. 验证list的底层数据结构

    <STL源代码剖析>中,指出SGI STL的list底层数据结构式循环双向链表.而且在链表尾端留一个空白节点.让end指向它.因为是双向的,那么list的迭代器必须是Bidirection ...

  3. UICollectionView——整体总结

    前言 这几天有时间看了下UICollectionView的东西,才发觉它真的非常强大,很有必要好好学习学习.以前虽然用过几次,但没有系统的整理总结过.这两天我为UICollectionView做一个比 ...

  4. sql中去掉换行符和回车符

    sql 中,char(13),char(10)或nchar(13),nchar(10)可表示SQL中的回车换行符,但是会以空格的形式显示.replace(replace(字段名,char(10), ' ...

  5. 商业模式(二):P2P网贷平台,利差和服务费为主的金融玩法

    2014~2015,先后在2家P2P平台工作过,还了解过其它若干武汉P2P平台. 结合自己的工作经历和理财经历,说几句~ 1.P2P网贷这种金融类的创业项目和经营风险,远高于制造业和服务业~      ...

  6. 03010_防止SQL注入

    1.预处理对象 (1)使用PreparedStatement预处理对象时,建议每条sql语句所有的实际参数,都使用逗号分隔: String sql = "insert into sort(s ...

  7. [Angular] Create a custom validator for reactive forms in Angular

    Also check: directive for form validation User input validation is a core part of creating proper HT ...

  8. 用 OPENSSL 生成不同格式的密钥

    用 OPENSSL 生成不同格式的密钥 密钥 key 值包括 加密算法: RSA/DSA/ECC 加密位数: 1024/2048/4096 密钥口令:加密方式有很多 在使用 DSA/ECC 加密算法时 ...

  9. eclipse个人插件

    1.SVN eclipse markets 安装m2e-subversion.svnkit 2.maven 本地装好mvn prefences导入maven安装目录和配置 3.单元测试覆盖率 EclE ...

  10. Qt之输出控制

    简述 在Qt项目开发过程中,往往需要对程序的一些信息进行控制,比如:打印日志.调试信息等,便于我们后期查找.跟踪及定位问题. 下面,我们来分享下常用的几种方式. 简述 示例代码 应用程序输出 控制台输 ...