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 ...
随机推荐
- android ActionBar的使用
Action Bar主要功能包括: 1. 显示选项菜单 2. 提供标签页的切换方式的导航功能,能够切换多个fragment. 3. 提供下拉的导航条目. 4. 提供交互式活动视图取 ...
- zoj 2317 Nice Patterns Strike Back(矩阵乘法)
problemId=1317">http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=1317 给出一个n*m的矩阵( ...
- Python: PS 滤镜特效 -- Marble Filter
本文用 Python 实现 PS 滤镜特效,Marble Filter, 这种滤镜使图像产生不规则的扭曲,看起来像某种玻璃条纹, 具体的代码如下: import numpy as np import ...
- 基于 Web 的 Go 语言 IDE - Wide 1.1.0 发布!
发布 1.1.0 这个版本改进了很多细节,已经完全可以用于正式项目的开发 同时我们上线了 Wide 在线服务 到目前,我们提供了 Wide 和 Solo 两个在线服务,详情请看这里. Wide 是什么 ...
- Nabou应用实例
本文接上文 <完整性检查工具Nabou> http://chenguang.blog.51cto.com/350944/280712650) this.width=650;" ...
- windows下go语言环境
1 liteIDE ,随便装哪儿都行 2 GO语言包 安装 ,我安装到了 c:\go (顺便给个地址下载地址 https://golang.org/dl/ ) 3 GCC编译器 安装,同 ...
- C# 开发 —— 数组类对象接口
数组类型是从抽象基类 Array 派生的引用类型,通过new运算符创建数组并将数组元素初始化为他们的默认值 一维数组 type[] arrayname; 数组的长度不是声明的一部分,而且数组必须在访问 ...
- Codefroces Educational Round 27 845G Shortest Path Problem?
Shortest Path Problem? You are given an undirected graph with weighted edges. The length of some pat ...
- C/C++(指针数组)
指针数组 指针数组的本质是数组,数组指针的本质是指针 一个数组中的各个元素都是字符指针,即为字符指针数组,或者指针数组. int arr[] = {1,2,23,45,6};//整形数组 char c ...
- java三元表达式编程规范问题
package day01; public class Program { public static void main(String[] args) { // TODO Auto-g ...