C++11 move_iterator
template<typename Iterator> class move_iterator
{
Iterator current;
public:
typedef Iterator iterator_type;
typedef typename std::iterator_traits<Iterator>::iterator_category iterator_category;
typedef typename std::iterator_traits<Iterator>::value_type value_type;
typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
typedef Iterator pointer; typedef value_type&& reference; move_iterator(){}
explicit move_iterator(Iterator it):current(it){}
template<typename Iter> move_iterator(const move_iterator<Iter>& it):current(it.current){}
template<typename Iter> move_iterator& operator=(const move_iterator<Iter>&it)
{
current = it.current;
}
iterator_type base() const{ return current;}
pointer operator->()const{return current;} reference operator*()const{return std::move(*current);} move_iterator&operator++(){++current; return *this;}
move_iterator&operator--(){--current; return *this;} move_iterator&operator++(int){move_iterator temp = *this; ++current; return temp;}
move_iterator&operator--(int){move_iterator temp = *this; --current; return temp;} move_iterator operator+(difference_type n) const {return move_iterator(current + n);}
move_iterator operator-(difference_type n) const {return move_iterator(current - n);}
move_iterator operator+=(difference_type n) {current += n; return *this;}
move_iterator operator-=(difference_type n) {current -= n; return *this;} auto operator[](difference_type n) const -> decltype(std::move(current[n]))
{
return std::move(current[n]);
} };
从实现上可以看出,基本是普通Iterator加了外包装。
其中特别值得注意的几个点:
typedef value_type&& reference; auto operator[](difference_type n) const -> decltype(std::move(current[n]))
{
return std::move(current[n]);
} reference operator*()const{return std::move(*current);}
可以发现,这种迭代器呈现出差别就在解引用的时候强制转换成右值引用,这样就可以实现从一个容器中”移走“所有的元素
#include<vector>
#include<algorithm>
#include<stack>
#include<iostream>
#include<string>
using namespace std; int main()
{
std::vector<std::string> foo();
std::vector<std::string> bar{"one", "two", "three"}; typedef std::vector<std::string>::iterator Iter; std::copy(std::move_iterator<Iter>(bar.begin()),
std::move_iterator<Iter>(bar.end()), foo.begin()); bar.clear();
std::cout << "foo:";
for(std::string& x : foo) cout << ' ' << x;
std::cout << '\n';
return ;
}
对于这个程序,可以跟踪一下,发现最后bar在copy之后,虽然仍旧含有三个元素,但都变成了"",可见已经被“掏空”了
后面那个clear只是使得bar的size减到0而已,可谓多此一举。
所有的差别,不过是因为我们输入参数用move_iterator裹了一层:std::move_iterator<Iter>(bar.begin())
还有就是move_iterator是模板类,不是模板函数,所以需要显式写出模板参数,在这里就显得有些啰嗦了。
还可以看一下另一个关于解引用的例子
#include<iterator>
#include<string>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
std::string str[] = {"one", "two", "three"};
std::vector<std::string> foo; std::move_iterator<std::string*> it(str);
for(int i = ; i < ; i++)
{
foo.push_back(*it);
++it;
}
std::cout << "foo:";
for(std::string& x : foo) std::cout << " " << x;
std::cout << "\n";
return ;
}
因为普通指针也可以被iterator_traits识别为迭代器,所以也可以用move_iterator进行包装
C++11 move_iterator的更多相关文章
- 地区sql
/*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...
- C++11 std::copy
这个函数并不是简单的 while(first != last) { *result = *first; result++; first++; } 事实上这种写法是最具普适性的,值要求inputIter ...
- WinForm 天猫2013双11自动抢红包【源码下载】
1. 正确获取红包流程 2. 软件介绍 2.1 效果图: 2.2 功能介绍 2.2.1 账号登录 页面开始时,会载入这个网站:https://login.taobao.com/member/login ...
- C++11特性——变量部分(using类型别名、constexpr常量表达式、auto类型推断、nullptr空指针等)
#include <iostream> using namespace std; int main() { using cullptr = const unsigned long long ...
- CSS垂直居中的11种实现方式
今天是邓呆呆球衣退役的日子,在这个颇具纪念意义的日子里我写下自己的第一篇博客,还望前辈们多多提携,多多指教! 接下来,就进入正文,来说说关于垂直居中的事.(以下这11种垂直居中的实现方式均为笔者在日常 ...
- C++ 11 多线程--线程管理
说到多线程编程,那么就不得不提并行和并发,多线程是实现并发(并行)的一种手段.并行是指两个或多个独立的操作同时进行.注意这里是同时进行,区别于并发,在一个时间段内执行多个操作.在单核时代,多个线程是并 ...
- CSharpGL(11)用C#直接编写GLSL程序
CSharpGL(11)用C#直接编写GLSL程序 +BIT祝威+悄悄在此留下版了个权的信息说: 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharp ...
- ABP(现代ASP.NET样板开发框架)系列之11、ABP领域层——仓储(Repositories)
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之11.ABP领域层——仓储(Repositories) ABP是“ASP.NET Boilerplate Proj ...
- C++11 shared_ptr 智能指针 的使用,避免内存泄露
多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...
随机推荐
- servlet的配置和上下文
一.Servlet配置对象(ServletConfig):Servlet初始化时,容器调用其init(ServletConfig)方法,传递该对象. 1.获得对象方法: (1).直接使用getServ ...
- python 练习 15
#!/usr/bin/python # -*- coding: UTF-8 -*- for i in range(1,10): for j in range(1,10): result = i * j ...
- nginx配置 php 单入口
location / { root html; index index.html index.htm index.php; if (!-e $requ ...
- DSP bootloader学习笔记1
DSP bootloader学习笔记1 彭会锋 参考: TMS320F28xx DSP中内部Flash的应用研究 http://wenku.baidu.com/view/83e9837931b765c ...
- python中range和xrange的区别
1.range生成一个列表:xrange生成一个生成器 2.用法都差不多
- PHP Warning: ob_start() : output handler 'ob_gzhandler conflicts with 'zlib output compression'
安装phpcms过程中,会遇到Warning: ob_start() : output handler 'ob_gzhandler conflicts with 'zlib output compr ...
- ThinkPHP框架如何修改X-Powered-By
以前用ThinkPHP框架开发了一个小网站,前几天查询页面HTTP状态发现,里面有一项: X-Powered-By: ThinkPHP 2.0 这样虽然没什么,但感觉如果别有用心的人查询会知道你是用这 ...
- debug && release
http://www.cnblogs.com/awpatp/archive/2009/11/05/1597038.html Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调 ...
- PHP 页面编码声明方法详解(header或meta)
php的header来定义一个php页面为utf编码或GBK编码 php页面为utf编码 header("Content-type: text/html; charset=utf-8&quo ...
- BZOJ2494 Triangles and Quadrangle
一道非常"简单"的计算几何题... 题意:给你两个三角形和一个四边形,问你能否用这两个三角形拼成这个四边形 首先...四边形可能是凹四边形...需要判断一下...这个比较简单直接分 ...