理解iterator

参考:http://www.cplusplus.com/reference/std/iterator/iterator/

// iterator example
#include <iostream>
#include <iterator>
using namespace std;

class myiterator : public iterator<input_iterator_tag, int>
{
	int* p;
public:
	myiterator(int* x) :p(x) {}
	myiterator(const myiterator& mit) : p(mit.p) {}
	myiterator& operator++() {++p;return *this;}
	myiterator operator++(int) {myiterator tmp(*this); operator++(); return tmp;}
	bool operator==(const myiterator& rhs) {return p==rhs.p;}
	bool operator!=(const myiterator& rhs) {return p!=rhs.p;}
	int& operator*() {return *p;}
};

int main () {
	int numbers[]={10,20,30,40,50};
	myiterator beginning(numbers);
	myiterator end(numbers+5);
	for (myiterator it=beginning; it!=end; it++)
		cout << *it << " ";
	cout << endl;
	
	return 0;
}
//Output: 10 20 30 40 50

MSDN中的iterator

iterator
template<class C, class T, class Dist = ptrdiff_t>
struct iterator {
    typedef C iterator_category;
    typedef T value_type;
    typedef Dist distance_type;
};
The template class serves as a base type for all iterators. It defines the member types 
iterator_category (a synonym(synonym同义词) for the template parameter C), 
value_type (a synonym for the template parameter T), 
and distance_type (a synonym for the template parameter Dist).

表示iterator是其他所有迭代器的一个基本类型。

来自 http://www.cplusplus.com/reference/std/iterator/iterator/中的iterator

iterator 是一个类模板。This is a base class template that can be used to derive iterator classes from it. It is not an iterator class and does not provide any of the functionality an iterator is expected to have.

This base class only provides some member types, which in fact are not required to be present in any iterator type (iterator types have no specific member requirements), but they might be useful, since they define the members needed for the default iterator_traits class template to generate the appropriate iterator_traits class automatically.

iterator的定义如下:

template <class Category, class T, class Distance = ptrdiff_t,
          class Pointer = T*, class Reference = T&>
  struct iterator {
    typedef T         value_type;
    typedef Distance  difference_type;
    typedef Pointer   pointer;
    typedef Reference reference;
    typedef Category  iterator_category;
  };
参数说明如下:

T
Type of elements pointed by the iterator.
Distance
Type to represent the difference between two iterators.
Pointer
Type to represent a pointer to an element pointed by the iterator.
Reference
Type to represent a reference to an element pointed by the iterator.
Category
Category to which the iterator belongs to. It must be one of the following iterator tags:
iterator tag Category of iterators
input_iterator_tag Input Iterator
output_iterator_tag Output Iterator
forward_iterator_tag Forward Iterator
bidirectional_iterator_tag Bidirectional Iterator
random_access_iterator_tag Random Access Iterator

请参见本文最上面的例子。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值