ITERATOR 迭代器

template<class InputIterator,class T>

InputIterator find(InputIterator first,InputIterator last,const T& value)

{

  while(first != last && *first != value)

    ++first;

  return first;

}

代码示例

 #include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <algorithm>
#include <iostream> using namespace std; int main(int argc, char *argv[])
{
const int arraySize = ;
int ia[arraySize] = {,,,,,,}; vector<int> ivect(ia,ia+arraySize);
list<int> ilist(ia,ia+arraySize);
deque<int> ideque(ia,ia+arraySize); vector<int>::iterator it1 = find(ivect.begin(),ivect.end(),);
if(it1 == ivect.end())
cout << "4 not found." << endl;
else
cout << "4 found. " << * it1 << endl; list<int>::iterator it2 = find(ilist.begin(),ilist.end(),);
if(it2 == ilist.end())
cout << "6 not found. " << endl;
else
cout << "6 found. " << *it2 << endl; deque<int>::iterator it3 = find(ideque.begin(),ideque.end(),);
if(it3 == ideque.end())
cout << "8 not found. " << endl;
else
cout << "8 find " << *it3 << endl; return ;
}

stl中容器有vector\set\list等等等等

算法有find\count等

两者独立 而他们之间的联系便是由iterator进行连接 将两者粘合起来

iterator类似智能指针

智能指针auto_ptr 除了拥有平常指针概念的功能 还具有引用计数功能

通过对该指针指向的元素的引用计数 自动释放元素内存资源 而不必手动调用delete

(auto_ptr 在c++11之后已经被智能指针shared_ptr unique_ptr取代)

示例代码如下

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <algorithm>
#include <iostream> using namespace std; template<class T>
class auto_ptr{
public:
explicit auto_ptr(T* p = 0):pointer(p){}
template<typename U>
auto_ptr(auto_ptr<U>& rhs):pointer(rhs.release()){}
~auto_ptr(){ cout << "enter delete status\n";delete pointer;} template<class U>
auto_ptr<T>& operator=(auto_ptr<U>& rhs){
if(this != &rhs) reset(rhs.release());
return *this;
}
T& operator*()const{return *pointer;}
T* operator->()const{return pointer;}
T* get()const{return pointer;} private:
T* pointer;
}; int main(int argc, char *argv[])
{
auto_ptr<string> ps(new string("test"));
cout << *ps << endl;
cout << ps->size() << endl;
return 0;
}

  

要使用iterator这个智能指针 就需要识别指向的元素的相关信息,比如类别、引用等

代码使用了trait技巧将元素信息提取出来

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <algorithm>
#include <iostream>
#include <typeinfo> using namespace std; struct INT{
typedef int value_type;
typedef int difference_type;
typedef int* pointer;
typedef int& reference;
}; struct FLOAT{
typedef float value_type;
typedef float difference_type;
typedef float* pointer;
typedef float& reference;
}; template<class I>
struct Iterator_Traits{
//typedef typename I::iterator_category iterator_category;
typedef typename I::value_type value_type;
typedef typename I::difference_type difference_type;
typedef typename I::pointer pointer;
typedef typename I::reference reference;
}; int main(int argc, char *argv[])
{
std::cout << typeid(Iterator_Traits<INT>::reference).name() << std::endl;
std::cout << typeid(Iterator_Traits<FLOAT>::reference).name() << std::endl; return 0;
}

  

至此 除了

//typedef typename I::iterator_category iterator_category;

还没解决 其他都解决完毕

iterator_category是什么东西呢?

iterator迭代器也是有类型区分的

那么在实际代码中是如何进行识别呢?

在代码执行时才识别区分 效率太低

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <algorithm>
#include <iostream>
#include <typeinfo> using namespace std; //申请五个作为迭代器iterator类别的结构
struct input_iterator_tag_{};
struct output_iterator_tag_{};
struct forward_iterator_tag_:public input_iterator_tag_{};
struct bidirectional_iterator_tag_:public forward_iterator_tag_{};
struct random_access_iterator_tag_:public bidirectional_iterator_tag_{}; struct INT{
typedef input_iterator_tag_ iterator_category;
typedef int value_type;
typedef int difference_type;
typedef int* pointer;
typedef int& reference;
}; struct FLOAT{
typedef output_iterator_tag_ iterator_category;
typedef float value_type;
typedef float difference_type;
typedef float* pointer;
typedef float& reference;
}; template<class I>
struct MyIterator_Traits{
typedef typename I::iterator_category iterator_category;
typedef typename I::value_type value_type;
typedef typename I::difference_type difference_type;
typedef typename I::pointer pointer;
typedef typename I::reference reference;
};
template<typename T,typename Distance>
void test(T t,Distance n){
typename MyIterator_Traits<T>::iterator_category SELECT_TYPE;
test_(t,n,SELECT_TYPE);
} template<typename InputIterator,typename Distance>
void test_(InputIterator i,Distance j,input_iterator_tag_){
cout << "input_iterator_tag_" << endl;
} template<typename InputIterator,typename Distance>
void test_(InputIterator i,Distance j,output_iterator_tag_){
cout << "output_iterator_tag_" << endl;
} int main(int argc, char *argv[])
{
INT i;
FLOAT f;
char c;
test(i,c);
test(f,c); return 0;
}

  我们对不同的迭代器 指定不同的tag 这样就会进入到不同的函数中去了

 

c++ stl源码剖析学习笔记(二)iterator的更多相关文章

  1. c++ stl源码剖析学习笔记(一)uninitialized_copy()函数

    template <class InputIterator, class ForwardIterator>inline ForwardIterator uninitialized_copy ...

  2. c++ stl源码剖析学习笔记(三)容器 vector

    stl中容器有很多种 最简单的应该算是vector 一个空间连续的数组 他的构造函数有多个 以其中 template<typename T> vector(size_type n,cons ...

  3. STL源码剖析 学习笔记 MiniSTL

    https://github.com/joeyleeeeeee97 目录: 第二章 空间适配器 第三章 迭代器 第四章 序列式容器(vector,list,deque,stack,heap,prior ...

  4. STL源码剖析-学习笔记

    1.模板是一个公式或是蓝图,本身不是类或是函数,需进行实例化的过程.这个过程是在编译期完成的,编译器根据传递的实参,推断出形参的类型,从而实例化相应的函数 2. 后续补充-.

  5. STL源码剖析读书笔记之vector

    STL源码剖析读书笔记之vector 1.vector概述 vector是一种序列式容器,我的理解是vector就像数组.但是数组有一个很大的问题就是当我们分配 一个一定大小的数组的时候,起初也许我们 ...

  6. 重温《STL源码剖析》笔记 第三章

    源码之前,了无秘密. --侯杰 第三章:迭代器概念与traits编程技法 迭代器是一种smart pointer auto_Ptr 是一个用来包装原生指针(native pointer)的对象,声明狼 ...

  7. STL源码剖析读书笔记--第四章--序列式容器

    1.什么是序列式容器?什么是关联式容器? 书上给出的解释是,序列式容器中的元素是可序的(可理解为可以按序索引,不管这个索引是像数组一样的随机索引,还是像链表一样的顺序索引),但是元素值在索引顺序的方向 ...

  8. 重温《STL源码剖析》笔记 第五章

    源码之前,了无秘密  ——侯杰 序列式容器 关联式容器 array(build in) RB-tree vector set heap   map priority-queue multiset li ...

  9. 重温《STL源码剖析》笔记 第六、七、八章 next_permutation (字典序)

    源码之前,了无秘密  ——侯杰 第六章算法 next_permutation 比如:01342 -> 01423 -> 01432 方法:从尾端开始往前寻找两个相邻的元素,令第一个元素为* ...

随机推荐

  1. EXCEL自动撤销合并单元格并填充相应内容(转帖)

    若EXCEL工作表有很多合并的单元格,要将所有合并的单元格撤销,并填充撤销合并前显示的内容,这是一项很繁琐且容易出错的工作.但可通过宏程序可轻松准确地搞定,方法如下: 一.实现该功能的Excel宏程序 ...

  2. 黄聪:如何扩展Chrome DevTools来获取页面请求

    1. Chrome DevTools Extension 熟悉React的同学,可能对React Developer Tools并不陌生,     刚看到的时候,我也觉得很神奇, 因为React De ...

  3. 猴子选大王的c#实现

    原文地址:猴子选大王的c#实现作者:余文 今天被问到了猴子选大王的意思,题目大意就是说有n只猴子围坐成一个圈,按顺时针方向从1到n编号.然后从1号猴子开始沿顺时针方向从1开始报数,报到m的猴子出局,再 ...

  4. Kafka 如何读取offset topic内容 (__consumer_offsets)(转发)

    原文  https://www.cnblogs.com/huxi2b/p/6061110.html 众所周知,由于Zookeeper并不适合大批量的频繁写入操作,新版Kafka已推荐将consumer ...

  5. Python Day5 模块 包

    一:区分Python文件的2种用途 1个Python文件的2种用途 1.1 当作脚本执行:        if __name__ == '__main__': 1.2 当作模块导入使用     if ...

  6. matplotlib绘图总结《转》

    本文作为学习过程中对matplotlib一些常用知识点的整理,方便查找. 类MATLAB API 最简单的入门是从类 MATLAB API 开始,它被设计成兼容 MATLAB 绘图函数. from p ...

  7. ORA-00911: invalid character 包含中文报错

    SQL在pl下正常执行在vs里报错ORA-00911: invalid character. 1.检查SQL末尾是否含有";" 去掉 2.sql包含中文报错 string sql ...

  8. HashMap的实现原理,以及在JDK1.7和1.8的区别

    1.JDK1.7 HashMap是Java中大家最常用的一个map实现类,其为键值对也就是key-value的形式.他的数据结构则是采用的位桶和链表相结合的形式完成了,即拉链法.具体如下图所示: Ha ...

  9. lambda表达式,filter,map,reduce,curry,打包与解包和

    当然是函数式那一套黑魔法啦,且听我细细道来. lambda表达式 也就是匿名函数. 用法:lambda 参数列表 : 返回值 例: +1函数 f=lambda x:x+1 max函数(条件语句的写法如 ...

  10. ccc切割刚体

    // http://www.emanueleferonato.com/2011/08/05/slicing-splitting-and-cutting-objects-with-box2d-part- ...