Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pair of begin and end iterator -- does it have a name?

Tags:

c++

iterator

In C++, is there a struct (or class) defined representing a pair of iterators -- one the beginning and one the ending iterator? What's best practice to represent that? std::pair? I know that I can easily build that myself, but I would like to follow common practice.

I search for the following:

template<class It>
struct XXX {
private:
   It b;
   It e;
public:
   It begin () const { return b; }
   It end () const { return e; }
   // ...
};
like image 787
JohnB Avatar asked Nov 28 '12 11:11

JohnB


People also ask

What does the end iterator point to?

In something like an std::vector the ::end() iterator will point to one past the last element. You can't dereference this iterator but you can compare it to another iterator. If you compare another iterator to end() you know you've reached the end of the container.

How do I assign an iterator in C++?

Operator= -- Assign the iterator to a new position (typically the start or end of the container's elements). To assign the value of the element the iterator is pointing at, dereference the iterator first, then use the assign operator.

What is the Begin function in C++?

begin() function is used to return an iterator pointing to the first element of the vector container. begin() function returns a bidirectional iterator to the first element of the container.


2 Answers

If it's a pair of two arbitrary iterators it's just that - a pair of iterators.

If it happens to be a pair of iterators for which certain assumptions hold, such as "They point into the same container", I'd call it "Range" since that's what it's called throughout the documentation of the standard template library:

  • The SGI Introduction to the Standard Template Library writes 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 writes (yeah, I know the credibility of that site is rather poor, but anyway): 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.

  • The Working Draft for the C++ Standard writes in 24.2.1 paragraph 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

like image 66
Frerich Raabe Avatar answered Sep 27 '22 21:09

Frerich Raabe


Have a look at Boost.Range, in particular boost::iterator_range.

like image 34
Mankarse Avatar answered Sep 27 '22 21:09

Mankarse