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; }
// ...
};
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.
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.
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.
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
Have a look at Boost.Range, in particular boost::iterator_range.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With