加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 使用指针作为容器迭代器是否违反标准

发布时间:2020-12-16 04:59:58 所属栏目:百科 来源:网络整理
导读:Angew made a comment使用原始指针作为迭代器类型的向量很好.那种情绪让我陷入了困境. 我开始研究它并发现向量迭代器的要求只是它们是“Random Access Iterators”,它明确指出指针有资格: A pointer to an element of an array satisfies all requirements
Angew made a comment使用原始指针作为迭代器类型的向量很好.那种情绪让我陷入了困境.

我开始研究它并发现向量迭代器的要求只是它们是“Random Access Iterators”,它明确指出指针有资格:

A pointer to an element of an array satisfies all requirements

是编译器为调试目的提供向量迭代器的唯一原因,还是实际上我错过了向量?

解决方法

§ 24.2.1

Since iterators are an abstraction of pointers,their semantics is a generalization of most of the semantics
of pointers in C++. This ensures that every function template that takes iterators works as well with
regular pointers.

所以是的,使用指针满足随机访问迭代器的所有要求.

std :: vector可能出于几个原因提供迭代器

>标准说它应该.>如果std :: map或std :: set等容器提供了迭代器,而std :: vector只提供了value_type *指针,那就太奇怪了.迭代器提供容器库的一致性.>它允许矢量类型的特化,例如,std :: vector< bool>其中value_type *指针不是有效的迭代器.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读