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 ...
随机推荐
- [Codeforces558E]A Simple Task 线段树
链接 题意:给定一个长度不超过 \(10^5\) 的字符串(小写英文字母),和不超过5000个操作. 每个操作 L R K 表示给区间[L,R]的字符串排序,K=1为升序,K=0为降序. 最后输出最终 ...
- 项目: 更新(二) python 实现大概FTP的功能
服务器利用 socketserver 模块 构造, 实现了 多进程. 客户端仍然利用的是底层的 socket模块. 只不过进行了更深度的 解耦, 新加或者删除 某些功能 更方便 在上一个版本的基础上, ...
- sql创建外键
建立外键关系:先建主表再见从表:主表:create table zhu(code int parimary key,name varchar(20)) ;从表:create table cong(co ...
- 【Django】信号调度
Django中提供了"信号调度",用于在框架执行操作时解耦. 通俗来讲,就是在某些动作发生时,信号允许特定的发送者去提醒一些接受者. * Django内置信号:** Model s ...
- Java中Thread源码剖析
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 关于线程,用很长时间了,主线程下的子线程去做一些事情,就是一个代理模式,主线程分代理权给子线程,子线 ...
- understand软件使用教程(转)
源代码阅读工具(Scientific Toolworks Understand)的特色 1.支持多语言:Ada, C, C++, C#, Java, FORTRAN, Delphi, Jovial, ...
- HDU——T 1711 Number Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory Lim ...
- iOS -读书笔记-网络请求
知道"3次握手"吗?突然想起这个词 什么是3次握手? TCP三次握手/四次挥手详解 这里是3次握手的详解 3次握手就是为了可靠的传送数据,TCP(什么是TCP呢?TCP就是一种可靠 ...
- git -处理分支合并
1.分支间的合并 1)直接合并:把两个分支上的历史轨迹合二为一(就是所以修改都全部合并) zhangshuli@zhangshuli-MS-:~/myGit$ vim merge.txt zhangs ...
- 版本管理系统:svn和git
svn是常用的版本管理系统,解决团队协作开发和版本管理问题, 一.服务器端:是一个文件存储仓库,可以设置用户并管理其访问的权限.主要功能包括 ①设置文件存储路径,是管理文件版本的基础 ②设置用户:可以 ...