5

在 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; }
   // ...
};
4

2 回答 2

5

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

如果它碰巧是一对具有某些假设的迭代器,例如“它们指向同一个容器”,我将其称为“Range”,因为它在整个标准模板库的文档中都是这样称呼的:

  • SGI 标准模板库简介写道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.

  • C++ 标准的工作草案在 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

于 2012-11-28T11:15:10.220 回答
4

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

于 2012-11-28T11:15:27.597 回答