Iterator¶
The iterator header file contains template functions for working with iterators.
-
namespace utils
Functions
-
template<std::size_t N = 1, typename ...InputIts>
void advance(InputIts&... iterators)¶ Advances multiple iterators by a specified number of steps.
This function advances each of the provided iterators by N steps. The default value for N is 1.
- Template Parameters:
N – The number of steps to advance the iterators. Default is 1.
InputIts – Variadic template parameter for the types of the iterators.
- Parameters:
iterators – The iterators to be advanced.
-
template<typename InputIt1, typename InputIt2>
long long distance_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)¶ Computes the difference in distances between two ranges.
This function calculates the absolute difference between the distances of two given ranges. The first range is defined by a pair of iterators of the same types, and the second range is defined by a pair of iterators of possibly different types.
- Template Parameters:
InputIt1 – Input iterator type for the first range.
InputIt2 – Input an iterator type for the second range.
- Parameters:
first1 – Iterator to the beginning of the first range.
last1 – Iterator to the end of the first range.
first2 – Iterator to the beginning of the second range.
last2 – Iterator to the end of the second range.
- Returns:
The difference between the modulus of distances of the two ranges.
-
template<typename InputIt1, typename InputIt2>
auto get_longer_range(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)¶ Returns the range with the longer distance between two given ranges.
This function compares the distances of two ranges and returns the range with the longer distance. If the distances are equal, it returns the first range.
- Template Parameters:
InputIt1 – Input iterator type for the first range.
InputIt2 – Input an iterator type for the second range.
- Parameters:
first1 – Iterator to the beginning of the first range.
last1 – Iterator to the end of the first range.
first2 – Iterator to the beginning of the second range.
last2 – Iterator to the end of the second range.
- Returns:
A pair of iterators representing the range with the longer distance.
-
template<typename ...InputIts>
class MultiIterator¶ - #include <iterator.hpp>
A class template for iterating over multiple iterators simultaneously.
This class template allows for the simultaneous iteration over multiple iterators of potentially different types. It provides various operators to advance, compare, and access the elements pointed to by the iterators.
- Template Parameters:
InputIts – Variadic template parameter for the types of the iterators.
Public Types
-
using iterator_category = std::common_type_t<typename std::iterator_traits<InputIts>::iterator_category...>¶
The iterator category type.
-
using value_type = std::tuple<typename std::iterator_traits<InputIts>::value_type...>¶
The value type of the elements pointed to by the iterators.
-
using difference_type = std::common_type_t<typename std::iterator_traits<InputIts>::difference_type...>¶
The difference type for the iterators.
-
using pointer = std::tuple<typename std::iterator_traits<InputIts>::pointer...>¶
The pointer type to the elements pointed to by the iterators.
-
using reference = pointer_tuple<std::remove_reference_t<decltype(*std::declval<InputIts>())>...>¶
The reference type to the elements pointed to by the iterators.
Public Functions
-
MultiIterator() = default¶
-
MultiIterator(const MultiIterator &multi_iterator) = default¶
-
MultiIterator(MultiIterator &&multi_iterator) = default¶
-
inline explicit MultiIterator(InputIts&&... iterators)¶
Constructs a MultiIterator from multiple iterators.
- Parameters:
iterators – The iterators to be used for the MultiIterator.
-
MultiIterator &operator=(const MultiIterator &multi_iterator) = default¶
-
MultiIterator &operator=(MultiIterator &&multi_iterator) = default¶
-
~MultiIterator() = default¶
-
template<typename ...InputIters>
inline MultiIterator<InputIters...> make_multi_iterator(InputIters&&... iterators)¶ Creates a MultiIterator from multiple iterators.
- Template Parameters:
InputIters – Variadic template parameter for the types of the iterators.
- Parameters:
iterators – The iterators to be used for the MultiIterator.
- Returns:
A MultiIterator object.
-
inline reference operator*() const¶
Dereferences the MultiIterator to access the elements.
- Returns:
A reference to the elements pointed to by the iterators.
-
inline pointer operator->() const¶
Accesses the elements pointed to by the iterators.
- Returns:
A pointer to the elements pointed to by the iterators.
-
inline MultiIterator &operator++()¶
Pre-increment operator to advance the iterators.
- Returns:
A reference to the advanced MultiIterator.
-
inline MultiIterator operator++(int)¶
Post-increment operator to advance the iterators.
- Returns:
A copy of the MultiIterator before advancing.
-
inline MultiIterator &operator--()¶
Pre-decrement operator to move the iterators backward.
- Returns:
A reference to the MultiIterator after moving backward.
-
inline MultiIterator operator--(int)¶
Post-decrement operator to move the iterators backward.
- Returns:
A copy of the MultiIterator before moving backward.
-
inline MultiIterator &operator+=(difference_type d)¶
Advances the iterators by a specified distance.
- Parameters:
d – The distance to advance the iterators.
- Returns:
A reference to the advanced MultiIterator.
-
inline MultiIterator operator+(difference_type d) const¶
Returns a new MultiIterator advanced by a specified distance.
- Parameters:
d – The distance to advance the iterators.
- Returns:
A new MultiIterator advanced by the specified distance.
-
inline MultiIterator &operator-=(difference_type d)¶
Moves the iterators backward by a specified distance.
- Parameters:
d – The distance to move the iterators backward.
- Returns:
A reference to the MultiIterator after moving backward.
-
inline MultiIterator operator-(difference_type d) const¶
Returns a new MultiIterator moved backward by a specified distance.
- Parameters:
d – The distance to move the iterators backward.
- Returns:
A new MultiIterator moved backward by the specified distance.
-
inline auto operator-(const MultiIterator &rhs) const¶
Computes the difference between two MultiIterators.
- Parameters:
rhs – The right-hand side MultiIterator.
- Returns:
The difference between the two MultiIterators.
-
inline bool operator==(const MultiIterator &rhs) const¶
Equality comparison operator.
- Parameters:
rhs – The right-hand side MultiIterator.
- Returns:
True if the iterators are equal, false otherwise.
-
inline bool operator!=(const MultiIterator &rhs) const¶
Inequality comparison operator.
- Parameters:
rhs – The right-hand side MultiIterator.
- Returns:
True if all the iterators are different, false otherwise.
-
inline bool operator<(const MultiIterator &rhs) const¶
Less-than comparison operator.
- Parameters:
rhs – The right-hand side MultiIterator.
- Returns:
True if this MultiIterator is less than the right-hand side, false otherwise.
-
inline bool operator>(const MultiIterator &rhs) const¶
Greater-than comparison operator.
- Parameters:
rhs – The right-hand side MultiIterator.
- Returns:
True if this MultiIterator is greater than the right-hand side, false otherwise.
-
inline bool operator<=(const MultiIterator &rhs) const¶
Less-than-or-equal-to comparison operator.
- Parameters:
rhs – The right-hand side MultiIterator.
- Returns:
True if this MultiIterator is less than or equal to the right-hand side, false otherwise.
-
inline bool operator>=(const MultiIterator &rhs) const¶
Greater-than-or-equal-to comparison operator.
- Parameters:
rhs – The right-hand side MultiIterator.
- Returns:
True if this MultiIterator is greater than or equal to the right-hand side, false otherwise.
-
template<std::size_t N = 1, typename ...InputIts>
Usage¶
The following examples demonstrates how to use the iterator header file:
advance<std::size_t N = 1>
const std::vector vec1{1, 2, 3, 4, 5};
const std::list list1{1, 2, 3, 4, 5};
auto it1{vec1.begin()};
auto it2{list1.begin()};
utils::advance(it1, it2);
std::cout << "*it1: " << *it1 << '\n'
<< "*it2: " << *it2 << std::endl;
Output:
*it1: 2
*it2: 2
distance_difference
const std::vector vec1{1, 2, 3, 4, 5};
const std::vector vec2{1, 2, 3, 4, 5, 6};
const auto result{utils::distance_difference(vec1.begin(), vec1.end(),
vec2.begin(), vec2.end())};
std::cout << "result: " << result << std::endl;
Output:
result: -1
get_longer_range
const std::vector vec1{1, 2, 3, 4, 5, 6};
const std::vector vec2{1, 2, 3, 4, 5};
const auto &[first, last] = utils::get_longer_range(vec1.begin(), vec1.end(),
vec2.begin(), vec2.end());
std::cout << "partial_sum: ";
std::partial_sum(first, last, std::ostream_iterator<int>(std::cout, " "));
Output:
partial_sum: 1 3 6 10 15 21
MulitIterator
std::vector vec1{1, 2, 2, 1, 5, 6, 7, 8, 9, 10};
std::vector<std::string> vec2{"a", "b", "c", "d"};
utils::MultiIterator begin{vec1.begin(), vec2.begin()};
utils::MultiIterator end{vec1.end(), vec2.end()};
std::sort(begin, end, [](const auto &lhs, const auto &rhs) {
if (utils::get<0>(lhs) < utils::get<0>(rhs)) {
return true;
}
if (utils::get<0>(lhs) == utils::get<0>(rhs)) {
return utils::get<1>(lhs) < utils::get<1>(rhs);
}
return false;
});
for (auto it{begin}; it != end; ++it) {
std::cout << *it << " ";
}
Output:
[1, a] [1, d] [2, b] [2, c]