命名空间
变体
操作

std::basic_const_iterator

来自 cppreference.cn
< cpp‎ | iterator
 
 
迭代器库
迭代器概念
迭代器原语
算法概念和实用工具
间接可调用概念
常用算法要求
(C++20)
(C++20)
(C++20)
实用工具
(C++20)
迭代器适配器
basic_const_iterator
(C++23)
const_iterator
(C++23)
const_sentinel
(C++23)
make_const_iterator
(C++23)
make_const_sentinel
(C++23)

范围访问
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
 
定义于头文件 <iterator>
template< std::input_iterator Iter >
class basic_const_iterator;
(自 C++23 起)

std::basic_const_iterator 是一个迭代器适配器,其行为与底层迭代器完全相同(底层迭代器必须至少是 LegacyInputIterator 或模型 input_iterator),不同之处在于,解引用将底层迭代器返回的值转换为不可变的。 std::basic_const_iterator 的特化是常量迭代器,也就是说,该迭代器永远不能用作输出迭代器,因为不允许修改元素。

目录

[edit] 成员类型

成员类型 定义
iterator_category
(有条件地存在)

如果 Iter 建模 forward_iterator

否则,没有成员 iterator_category

iterator_concept
value_type std::iter_value_t<Iter>
difference_type std::iter_difference_t<Iter>
reference (private) std::iter_const_reference_t<Iter>
(仅为解释目的的成员类型*)

[edit] 成员对象

成员名称 定义
current (private) 底层迭代器,base() 从中复制或移动
(仅为解释目的的成员对象*)

[edit] 成员函数

构造一个新的 basic_const_iterator
(public member function) [edit]
访问底层迭代器
(public member function) [edit]
访问指向的元素
(public member function) [edit]
通过索引访问元素
(public member function) [edit]
递增或递减迭代器
(public member function) [edit]
转换为底层迭代器可转换的任何常量迭代器
(public member function) [edit]
比较底层迭代器
(public member function) [edit]

[edit] 非成员函数

比较 basic_const_iterator 与非 basic_const_iterator
(function template) [edit]
递增或递减迭代器
(function template) [edit]
(C++23)
计算两个迭代器适配器之间的距离
(function template) [edit]
(C++23)
将解引用底层迭代器的结果强制转换为其关联的右值引用类型
(function) [edit]

[edit] 辅助类

确定迭代器和适配的 basic_const_iterator 类型的通用类型
(class template specialization) [edit]

[edit] 辅助别名模板

template< std::input_iterator I >
using const_iterator = /* see description */;
(自 C++23 起)

如果 I 建模 constant-iterator(一个仅为解释目的的概念),则 const_iterator<I> 表示类型 I。 否则,basic_const_iterator<I>

template< std::semiregular S >
using const_sentinel = /* see description */;
(自 C++23 起)

如果 S 建模 input_iterator,则 const_sentinel<S> 表示类型 const_iterator<S>。 否则,S

[edit] 辅助函数模板

template< std::input_iterator T >
constexpr const_iterator<T> make_const_iterator( I it ) { return it; }
(自 C++23 起)
template< std::semiregular S >
constexpr const_sentinel<S> make_const_sentinel( S s ) { return s; }
(自 C++23 起)

[edit] 注解

特性测试 Std 特性
__cpp_lib_ranges_as_const 202207L (C++23) std::basic_const_iterator
202311L (C++23)
(DR)
std::basic_const_iterator 应遵循其底层类型的可转换性

[edit] 示例

#include <cassert>
#include <iterator>
#include <vector>
 
int main()
{
    std::vector v{1, 2, 3};
    std::vector<int>::iterator i = v.begin();
    *i = 4;   // OK, v[0] == 4 now
    i[1] = 4; // OK, the same as *(i + 1) = 4;
 
    auto ci = std::make_const_iterator(i);
    assert(*ci == 4);   // OK, can read the underlying object
    assert(ci[0] == 4); // OK, ditto
    // *ci = 13;        // Error: location is read-only
    // ci[0] = 13;      // Error: ditto
    ci.base()[0] = 42;  // OK, underlying iterator is writable
    assert(*ci == 42);  // OK, underlying location v[0] was modified
}

[edit] 缺陷报告

以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
P2836R1 C++23 basic_const_iterator 不遵循其底层类型的可转换性 提供转换运算符