std::common...mon_iterator(3)
πΈ NAME πΈ
This is the page for std::common_iterator::common_iterator β the manual header shows it as std::common...mon_iterator(3) β and it comes from the C++ Standard Libary manual. Yes, βLibaryβ with one r: iconic typo, no notes π
.
β¨ Synopsis β¨
constexpr common_iterator() requires std::default_initializable<I>
= default;
(1) (since C++20)
constexpr common_iterator( I i );
(2) (since C++20)
constexpr common_iterator( S s );
(3) (since C++20)
template< class I2, class S2 >
requires std::convertible_to<const I2&, I> &&
std::convertible_to<const S2&, S>
constexpr common_iterator( const common_iterator<I2, S2>& x );
(4) (since C++20)
Okay so bestie, this constructor is literally constructing a new iterator adaptor, and it effectively initializes the underlying std::variant<I, S> member object var so it holds an I (iterator) or an S (sentinel) object. Slay.
- Default constructor. Default-initializes
var. After construction,varholds a value-initializedIobject. Operations on the resulting iterator adaptor have defined behavior if and only if the corresponding operations on a value-initializedIalso have defined behavior. - After construction,
varholds anIobject move-constructed fromi. - After construction,
varholds anSobject move-constructed froms. - After construction,
varholds anIorSobject initialized from theI2orS2held byx.var, ifx.varholds that alternative, respectively. The behavior is undefined ifxis in an invalid state, that is,x.var.valueless_by_exception()is equal totrue.
π¨ No cap: that last one is undefined behavior when x.var.valueless_by_exception() == true, so donβt play with invalid states, bestie.
π Parameters π
iβ iterator to adaptsβ sentinel to adaptxβ iterator adaptor to copy
π Example π
// Run this code
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main()
{
std::vector v{3, 1, 4, 1, 5, 9, 2};
using CI = std::common_iterator<
std::counted_iterator<std::vector<int>::iterator>,
std::default_sentinel_t>;
CI unused; // (1)
CI start{std::counted_iterator{std::next(begin(v)), ssize(v) - 2}};
// (2)
CI finish{std::default_sentinel}; // (3)
CI first{start}; // (4)
CI last{finish}; // (4)
std::copy(first, last, std::ostream_iterator<int>{std::cout, " "});
std::cout << '\n';
std::common_iterator<
std::counted_iterator<
std::ostream_iterator<double>>,
std::default_sentinel_t>
beg{std::counted_iterator{std::ostream_iterator<double>{std::cout,";
"}, 5}},
end{std::default_sentinel};
std::iota(beg, end, 3.1);
std::cout << '\n';
}
π Output: π
1 4 1 5 9
3.1; 4.1; 5.1; 6.1; 7.1;
π See also π
operator=β assigns another iterator adaptor (C++20) (public member function)
Source/version/date sign-off: this page is from http://cppreference.com, version 2024.06.10, and itβs the std::common...mon_iterator(3) page. Bye bestie, go construct something iconic π
β¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms