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

任意の 2 つのイテレータのペアである場合は、イテレータのペアです。

「それらは同じコンテナーを指している」など、特定の仮定が成り立つ反復子のペアである場合、標準テンプレート ライブラリのドキュメント全体で呼ばれているため、「範囲」と呼びます。

  • 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.

  • 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 に答える