2012-11-28 27 views
4

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

回答

5

如果是一對,它只是兩個任意迭代器 - 一對迭代。

如果它碰巧是一對具有某些假設的迭代器,例如「它們指向同一個容器」,我會將它稱爲「範圍」,因爲這就是它在整個標準模板的文檔中所稱的庫:

  • SGI Introduction to the Standard Template LibraryFind 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