std::common_iterator - cppreference.com

From cppreference.com

template<std::input_or_output_iteratorI,std::sentinel_for<I>S>requires(!std::same_as<I,S>&&std::copyable<I>)classcommon_iterator;(since C++20)std::common_iterator is an iterator I / sentinel S adaptor that may represent a non-common range (where the types of I and S differ) as a

common_range

, by containing either an iterator or a sentinel, and defining the appropriate equality comparison operators operator==.

std::common_iterator can be used as a "bridge" between sequences represented by iterator/sentinel pair and legacy functions that expect

common_range

-like sequences.

Data members

Member name Definition varan object of type std::variant<I,S>
(exposition-only member object*)Member functions

Non-member functions

(C++20)

compares the underlying iterators or sentinels
(function template)

[edit]

(C++20)

computes the distance between two iterator adaptors
(function template)

[edit]

(C++20)

casts the result of dereferencing the underlying iterator to its associated rvalue reference type
(function)

[edit]

(C++20)

swaps the objects pointed to by two underlying iterators
(function template)

[edit]

Helper classes

Example

Run this code

#include<algorithm>#include<iostream>#include<iterator>#include<list>#include<string>template<classForwardIter>voidfire(ForwardIterfirst,ForwardIterlast){std::copy(first,last,std::ostream_iterator<std::string>{std::cout," "});}intmain(){std::list<std::string>stars{"Pollux","Arcturus","Mira","Aldebaran","Sun"};usingIT=std::common_iterator<std::counted_iterator<std::list<std::string>::iterator>,std::default_sentinel_t>;fire(IT(std::counted_iterator(stars.begin(),stars.size()-1)),IT(std::default_sentinel));}Output:

Pollux Arcturus Mira Aldebaran References

C++23 standard (ISO/IEC 14882:2024):

23.5.5 Common iterators [iterators.common]

C++20 standard (ISO/IEC 14882:2020):

23.5.4 Common iterators [iterators.common]

See also