STL algorithm算法mov,move_backward(38)
move原型:
std::move
template <class InputIterator, class OutputIterator>
OutputIterator move (InputIterator first, InputIterator last, OutputIterator result);
该函数是将指定范围内的元素移动到从result開始的位置。
move之后。[first,last)范围内的元素去留的详细实现由编译器决定。
result不能是在[first,last)范围内。
返回值为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)的更多相关文章
- STL algorithm算法merge(34)
merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...
- STL algorithm算法mismatch(37)
mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...
- STL algorithm算法is_permutation(27)
is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
- STL algorithm算法minmax,minmax_element(36)
minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T ...
- 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& ...
- 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& ...
- STL algorithm算法make_heap和sort_heap(32)
make_heap原型: std::make_heap default (1) template <class RandomAccessIterator> void make_heap ( ...
- STL algorithm算法lexicographical_compare(30)
lexicographical_compare原型: std::lexicographical_compare default (1) template <class InputIterator ...
随机推荐
- Intersection between 2d conic in OpenCASCADE
Intersection between 2d conic in OpenCASCADE eryar@163.com Abstract. OpenCASCADE provides the algori ...
- Icomparer和Icomparable集合排序
c#中实现对象集合的排序可以使用ArrayList中的Sort()方法,而有比较才能谈排序,因为不是基本类型(如string ,int.double......等)所以.NET Framework不可 ...
- CSS的导入方式:link与import方式的区别
在前端开发中,加载CSS样式文件有两种方式:link方式与import方式,它们之间的区别主要有以下几点: 1.兼容性不一样 link是一个HTML标签,所以它不存在兼容性问题,而import方式则具 ...
- 分享一个关于js原型链的理解
http://www.cnblogs.com/wyaocn/p/5815761.html
- HDU 4704 Sum 超大数幂取模
很容易得出答案就是2^(n-1) 但是N暴大,所以不可以直接用幂取模,因为除法操作至少O(len)了,总时间会达到O(len*log(N)) 显然爆的一塌糊涂 套用FZU1759的模板+顺手写一个大数 ...
- 解决create-react-app 后 npm start 中出现 的webpack版本问题和webpack-dev-server的版本问题
利用VSCode搭建react的脚手架运行环境的时候.create-react-app之后npm start出现如下图的问题: There might be a problem with the pr ...
- 删除D盘空目录 、检索大于10M的文件
删除D盘空目录 @echo off for %%i in (d:\xx) do ( if exist %%i:\ ( for /f "delims=" %%a in ('dir / ...
- 如何创建Hiren的BootCD USB磁盘 -- 制作U盘启动盘
如何创建Hiren的BootCD USB磁盘 原文 https://www.wintips.org/how-to-create-hirens-bootcd-usb-disk/ 本文基本是谷歌翻译 H ...
- 如果把父组件的数据实时的传递到子组件:用watch
1.在子组件使用watch来监听传递给子组件的数据,然后更新子组件的数据. 2.watch和computed结合使用效果非常好. 参考链接:https://blog.csdn.net/zhouweix ...
- VUE错误记录 - 小球模拟购物车
<body> <div id="app"> <input type="button" value="Add to Car ...