c++ stl源码剖析学习笔记(二)iterator

ITERATOR 迭代器

 

template<class InputIterator,class T>

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

{

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

    ++first;

  return first;

}

 

代码示例

 

复制代码
 1 #include <iostream>
 2 #include <vector>
 3 #include <list>
 4 #include <deque>
 5 #include <algorithm>
 6 #include <iostream>
 7 
 8 using namespace std;
 9 
10 int main(int argc, char *argv[])
11 {
12     const int arraySize = 7;
13     int ia[arraySize] = {0,1,2,3,4,5,6};
14 
15     vector<int> ivect(ia,ia+arraySize);
16     list<int> ilist(ia,ia+arraySize);
17     deque<int> ideque(ia,ia+arraySize);
18 
19     vector<int>::iterator it1 = find(ivect.begin(),ivect.end(),4);
20     if(it1 == ivect.end())
21         cout << "4 not found." << endl;
22     else
23         cout << "4 found. " << * it1 << endl;
24 
25     list<int>::iterator it2 = find(ilist.begin(),ilist.end(),6);
26     if(it2 == ilist.end())
27         cout << "6 not found. " << endl;
28     else
29         cout << "6 found. " << *it2 << endl;
30 
31     deque<int>::iterator it3 = find(ideque.begin(),ideque.end(),8);
32     if(it3 == ideque.end())
33         cout << "8 not found. " << endl;
34     else
35         cout << "8 find " << *it3 << endl;
36 
37 
38     return 0;
39 }
复制代码

 

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

算法有find\count等

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

iterator类似智能指针

 

 

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

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

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

示例代码如下

  

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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迭代器也是有类型区分的

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

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

 

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

 

 

posted on   itdef  阅读(301)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示