STL——迭代器的概念
迭代器是一种抽象的设计概念,现实程序语言中并没有直接对应于这个概念的实物。
1 迭代器设计思维——STL关键所在
不论是泛型思维或STL的实际运用,迭代器都扮演这重要的角色。STL的中心思想在于:将数据容器和算法分开,彼此独立设计,最后再以一贴胶着剂将它们撮合在一起。容器和算法的泛型化,从技术的角度来看是并不困难,C++的class template和function templates可分别达成目标。
以下是容器、算法、迭代器的合作展示,以算法find()为例,它接受两个迭代器和一个”搜索目标“:
template <class InputIterator,class T>
InputIterator find(InputIterator first,InputIterator last,const T& value)
{
while(first=!last&&*first!=value)
++first;
return first;
}
只要给出不同的迭代器,find()便能够对不同的容器进行直接操作:
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
#include<iostream>
using namespace std; int main()
{
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 found. "<<*it1<<endl;
else
cout<<"4 not found."<<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 found. "<<*it3<<endl;
}
从上面的例子看来,迭代器似乎依附于在容器之下,是吗?有没有独立而泛用的迭代器?我们又该如何自行设计特殊的迭代器?
2 迭代器是一种smart pointer
迭代器是一种行为类似指针的对象,而指针的各种行为中最常见也最重要的便是内容提领(dereference)和成员访问(member access),因此,迭代器最重要的编程工作就是对operator*和oper->进行重载工作。关于这一点,C++标准库有一个auto_ptr可供我们参考。这是一个用来包含原生指针的对象,声名狼藉的内存泄露问题可借此获得解决。auto_ptr用法如下,和原生指针一模一样:
void func()
{
auto_ptr<string> ps(new string("jjhou");
cout<<*ps<<endl;
cout<<ps->size()<<endl;
//离开前不需要delete,auto_ptr会自动释放内存
}
函数第一行的意思是,以算式new 动态配置一个初值为"jjhou"的string对象,并将所得的结果(一个原生指针)作为auto_ptr<string> 对象的初值。注意,auto_ptr尖括号内放的是”原生指针所指对象“的型别,而不是原生指针的型别。
auto_ptr的源代码在头文件<memory>中:
//file:autoptr.cpp template<class T>
class auto_ptr{
public:
explicit auto_ptr(T *p=):pointee(p) {}
template<class U>
auto_ptr(auto_ptr<U>& rhs):pointee(rhs.release()) {}
~auto_ptr() {delete pointee;} template<class U>
auto_ptr<T>& operator=(auto_ptr<U> &rhs)
{
if(this!=rhs) reset(ths.release());
return *this;
} T& operator*() const { return *pointee;}
T* operator->() const { return pointee;}
T* get() const {return pointee;}
//...
private:
T *pointee;
};
有了模仿对象,现在我们来为list(链表)设计一个迭代器,假设list机器节点的结构如下:
STL——迭代器的概念的更多相关文章
- STL 容器的概念
STL 容器的概念 在实际的开发过程中,数据结构本身的重要性不会逊于操作于数据结构的算法的重要性,当程序中存在着对时间要求很高的部分时,数据结构的选择就显得更加重要. 经典的数据结构数量有限,但是我们 ...
- 【STL 源码剖析】浅谈 STL 迭代器与 traits 编程技法
大家好,我是小贺. 点赞再看,养成习惯 文章每周持续更新,可以微信搜索「herongwei」第一时间阅读和催更,本文 GitHub : https://github.com/rongweihe/Mor ...
- STL迭代器笔记
STL迭代器简介 标准模板库(The Standard Template Library, STL)定义了五种迭代器.下面的图表画出了这几种: input output \ ...
- 一步一步的理解C++STL迭代器
一步一步的理解C++STL迭代器 "指针"对全部C/C++的程序猿来说,一点都不陌生. 在接触到C语言中的malloc函数和C++中的new函数后.我们也知道这两个函数返回的都是一 ...
- STL 迭代器 iterator const
STL迭代器很多时候可以当成指针来使用. 但是指针一般可以用const来控制访问. 那迭代器呢. #include <iostream> #include <vector> u ...
- STL迭代器的使用、正向、逆向输出双向链表中的所有元素
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- stl迭代器原理
具体实现肯定不如书上讲的清楚了,这里只是根据侯捷书上的讲解,自己建立一条思路以及形成一些相关的概念 迭代器也可被称作智能指针,用于遍历容器内的元素,stl每个容器都实现了自己的iterator,ite ...
- STL——迭代器与traits编程技法
一.迭代器 1. 迭代器设计思维——STL关键所在 在<Design Patterns>一书中对iterator模式定义如下:提供一种方法,使之能够依序巡访某个聚合物(容器)所含的各个元素 ...
- STL迭代器之一:偏特化
在stl的算法中运用容器的迭代器时,很可能经常会用到迭代器相应型别(例如迭代器所指物的型别),假设算法中有必要声明一个变量,以"迭代器所指对象的型别"为类型,如何是好,例如我们写一 ...
随机推荐
- jwplayer 网页在线播放插件
1.到官网 https://www.jwplayer.com/ 注册,取得key并下载免费版本(免费版只支持mp4格式): 2.编辑如下网页即可在线播放: <!DOCTYPE html> ...
- 【Jenkins】linux下Jenkins集成ant进行编译并发送结果
三个文章吧: 1 如何使用ant编译执行jmeter测试用例,并生成html报告 2 如何在Linux下搭建jenkins环境. 3 如何在Linux下搭建的jenkins中执行ant构建运行,并发送 ...
- 【转】C/C++除法实现方式及负数取模详解
原帖:http://blog.csdn.net/sonydvd123/article/details/8245057 一.下面的题目你能全做对吗? 1.7/4=? 2.7/(-4)=? 3.7%4=? ...
- 类内const static(static const)成员变量初始化问题
在查找const相关资料的过程中,又遇到了另外一个问题,就是C++类中const static(或者static const)成员变量应当如何初始化的问题. 查阅了许多资料,发现VC环境下,只允许co ...
- CSS换行:word-wrap、word-break和text-wrap区别
一.word-wrap:允许对长的不可分割的单词进行分割并换行到下一行.(中英文处理效果一样) word-wrap有两个取值: 1.word-wrap: normal:只在允许的断字点换行(浏览器保持 ...
- leetcode@ [54/59] Spiral Matrix & Spiral Matrix II
https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...
- ovirt user guide
Contents [hide] 1 Accessing the User Portal 1.1 Logging in to the User Portal 1.2 Logging out of t ...
- Android问题-DelphiXE5编义时提示找不到“连接器(arm-linux-androideabi-ld.exe)"
问题现象:DelphiXE5编义时提示找不到“连接器(arm-linux-androideabi-ld.exe)" 问题提示:Checking project dependencies... ...
- Property cannot be found on forward class object?
I have a UIView and I'm trying to set its layer properties. self.colorSwatch = [[UIView alloc] initW ...
- Jquery ajax 得到返回值
Jquery ajax 得到返回值 1.ajax默认是异步调用的,所以得到的返回值是空值,要得到值必须改成同步:async: false,//同步. 2.必须定义一个全局变量 var result = ...