chsvlib
chsv helper source code

◆ offset_iterator_end()

constexpr auto Chusov::Memory::offset_iterator_end ( Iterator  it,
difference_type  offset 
)
constexprnoexcept

Returns an object of an unspecified type which is an iterator of the same category as the parameter but redefines the respective comparison operators to keep the current offset from the initial position given parametrically.

Template Parameters
IteratorA type of it. It can be any iterator satisfying the Iterator requirements, although the effect of calling any iterator-related operators other than the comparison operators is not defined.
Parameters
itAn iterator to adapt to a counting iterator.
offsetA numeric offset, in iterated elements, of a position for the created iterator to correspond to. The position is specified relatively to it. The type of the parameter is either
typename std::iterator_traits<Iterator>::difference_type
or, if the latter yields void, e.g. for output iterators, the type of offset defaults to std::ptrdiff_t.
Returns
An iterator of unspecified type of the same category as it which delegates all calls except for the comparison operators to it and redefines the comparison operators to take into account an offset of the iterator from the initial position which corresponds to it shifted to the right (i.e. incremented) offset times. The returned iterator provides operators for changing its position and dereferencing, but the effect of invoking these operators, e.g. any operatiors other than comparison operators and assignment, is undefined.

The offset_iterator_begin and offset_iterator_end functions allow iterations inside code that has a range based interface (i.e. one that given by a pair of a beginning and an ending iterator) based on only a given beginning iterator and a number of elements to read, even when the given iterator is an InputIterator or an OutputIterator.

The function is marked noexcept if a copy-construction of it is noexcept.

#include <cstddef>
#include <vector>
#include <iostream>
#include <iterator>
template <class Iterator>
void show_all(Iterator begin, Iterator end)
{
for (auto it = begin; it != end; ++it)
std::cout << *it;
}
template <class Iterator>
void show_all(Iterator iter, std::size_t count)
{
using namespace Chusov::Memory;
show_all(offset_iterator_begin(iter), offset_iterator_end(iter, count));
}
int main(int, char**)
{
std::vector<int> v = {1, 2, 3, 4, 5};
show_all(std::begin(v) + 1, v.size() - 2);
std::cout << '\n';
return 0;
}
Advanced functionality and C++ objects for working with memory.
constexpr auto offset_iterator_end(Iterator it, difference_type offset) noexcept
Returns an object of an unspecified type which is an iterator of the same category as the parameter b...
constexpr unspecified offset_iterator_begin(Iterator it) noexcept
Returns an object of an unspecified type which is an iterator of the same category as the parameter b...
A namespace of functions and components used to work with memory.
Definition: chsvmem.h:71

Output:

234
See also
offset_iterator_end