std::common_iterator
来自cppreference.com
在标头 <iterator> 定义
|
||
template< std::input_or_output_iterator I, std::sentinel_for<I> S > requires ( !std::same_as<I, S> && std::copyable<I> ) |
(C++20 起) | |
std::common_iterator
是一种迭代器 I
/ 哨位 S
适配器,可以通过包含迭代器或哨位之一,并定义适合的比较运算符 operator==,从而将非公共范围(其中 I
与 S
不同)表示成一种 common_range
。
std::common_iterator
可以作为由迭代器/哨位对所表示的序列与期待 common_range
式序列的遗留函数之间的“桥梁”。
数据成员
成员名 | 定义 |
var
|
std::variant<I, S> 类型的对象 (仅用于阐述的成员对象*) |
成员函数
(C++20) |
构造新的迭代器适配器 (公开成员函数) |
(C++20) |
赋值另一迭代器适配器 (公开成员函数) |
(C++20) |
访问被指向的元素 (公开成员函数) |
(C++20) |
推进迭代器适配器 (公开成员函数) |
非成员函数
(C++20) |
比较底层迭代器或哨位 (函数模板) |
(C++20) |
计算两个迭代器适配器间的距离 (函数模板) |
(C++20) |
将解引用底层迭代器的结果转换为其关联的右值引用类型 (函数) |
(C++20) |
交换两个底层迭代器所指向的对象 (函数模板) |
辅助类
计算 std::common_iterator 类型的关联差类型 (类模板特化) | |
对 std::common_iterator 类型的属性提供统一的接口 (类模板特化) |
示例
运行此代码
#include <algorithm> #include <iostream> #include <iterator> #include <list> #include <string> template<class ForwardIter> void fire(ForwardIter first, ForwardIter last) { std::copy(first, last, std::ostream_iterator<std::string>{std::cout, " "}); } int main() { std::list<std::string> stars{"Pollux", "Arcturus", "Mira", "Aldebaran", "Sun"}; using IT = 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)); }
输出:
Pollux Arcturus Mira Aldebaran
引用
- C++23 标准(ISO/IEC 14882:2024):
- 23.5.5 Common iterators [iterators.common]
- C++20 标准(ISO/IEC 14882:2020):
- 23.5.4 Common iterators [iterators.common]
参阅
(C++20) |
指定范围拥有相同的迭代器和哨位类型 (概念) |
转换 view 为 common_range (类模板) (范围适配器对象) |