gpt4 book ai didi

c++ - `iterator` 和 `const_iterator` 不是 STL 容器的必需成员吗?

转载 作者:行者123 更新时间:2023-12-02 03:10:16 25 4
gpt4 key购买 nike

取自 Understanding iterator/const_iterator implementation :

"although iterator and const_iterator are types declared in the scope of vector, there is no requirement that vector(or any STL container) have a member of either type - iterator and const_iterator are part of the interface of std::vector e.g. overloads of the member begin() returns those types, but nothing is said about how those function obtain the iterator they return"

Additionally STL containers must have:

"a begin and end function that returns iterators"

上面指出iteratorconst_iterator不是 STL 容器的必需成员,例如 vector 。我认为这意味着从 .begin 返回的类型或.end根据实现情况会有所不同。

所以我想知道为什么这没有问题,因为我看到很多人写出 std::vector<someType>::iteratorstd::vector<someType>::const_iterator哪里iteratorconst_iterator指定而不是使用 auto例如:

for (std::vector<int>::iterator i = s.begin(); i != s.end(); i++)
{

}

最佳答案

您读错了引文。该人说

have a member of either type

不是

have either type

当他们说拥有任一类型的成员时,他们的意思是类中没有iteratorconst_iterator类型的数据成员。

他们确实接着说

iterator and const_iterator are part of the interface of std::vector

这是正确的,因为标准要求 std::vector 在其接口(interface)中显示这些类型。

关于c++ - `iterator` 和 `const_iterator` 不是 STL 容器的必需成员吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59760599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com