一对开始和结束迭代器 - 它有一个名字吗?

时间:2012-11-28 11:12:06

标签: c++ iterator

在C ++中,是否有struct(或类)定义了一对迭代器 - 一个是开头,一个是结束迭代器?代表这种情况的最佳做法是什么? std::pair?我知道我可以轻松地自己构建,但我想遵循惯例。

我搜索以下内容:

template<class It>
struct XXX {
private:
   It b;
   It e;
public:
   It begin () const { return b; }
   It end () const { return e; }
   // ...
};

2 个答案:

答案 0 :(得分:5)

如果它是一对两个任意迭代器,那就是 - 一对迭代器。

如果它碰巧是一对迭代器,某些假设成立,例如“它们指向同一个容器”,我称之为“Range”,因为它是标准模板库文档中的所谓:

  • SGI Introduction to the Standard Template Library撰写Find takes three arguments: two iterators that define a range, and a value to search for in that range. It examines each iterator in the range [first, last), proceeding from the beginning to the end, and stops either when it finds an iterator that points to value or when it reaches the end of the range.

  • cplusplus.com写道(是的,我知道该网站的可信度相当差,但无论如何):A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers.

  • Working Draft for the C++ Standard在24.2.1第7段中写道:A range is a pair of iterators that designate the beginning and end of the computation. A range [i,i) is an empty range; in general, a range [i,j) refers to the elements in the data structure starting with the element pointed to by i and up to but not including the element pointed to by j

答案 1 :(得分:4)

请查看Boost.Range,特别是boost::iterator_range