这个问题在这里已经有了答案:
Why is std::iterator deprecated?
(1 个回答)
8 个月前关闭。
听说std::iterator
在 C++17 中已弃用。
例如,来自 <algorithm>
的类似函数,更有可能我们将使用 begin()
和 end()
返回迭代器的对象的成员函数,如 std::string
, std::vector
, 等等。
或者像基于范围的 for 循环,我们需要 begin()
和 end()
也返回迭代器。
因此,如果 std::iterator
由于基类已被弃用,我们甚至应该使用像 begin()
这样的成员函数和 end()
或者在 STL 中使用其他需要迭代器的函数?
最佳答案
For example, like functions from
<algorithm>
没有来自
<algorithm>
的功能要求 std::iterator
.尽管 std::iterator
已弃用,但仍然可以使用它们.more likely we're going to be using begin() and end() member functions
这些都不需要使用
std::iterator
.which return iterator as well.
不推荐使用迭代器。类(class)模板
std::iterator
已弃用。should we even use member functions like begin() and end() or use other functions in the STL that require an iterator?
是的。虽然,我会建议通过
std::ranges
使用它们。方便时(在 C++20 中)。So iterators inside of object/class like
std::vector<int>::iterator
are not inherited fromstd::iterator
?
标准库迭代器类型由标准库实现定义。从技术上讲,它们可以继承自
std::iterator
但它们不是必需的,也不是保证从它继承的。他们是否继承自std::iterator
与否,它们本身并没有被弃用。std::iterator
过去打算用作自定义迭代器的基类。除非您正在定义自定义迭代器,否则您一开始就不会使用它。如果您正在定义自定义迭代器,则不应使用 std::iterator
了。请注意,从来没有必要使用 std::iterator
以此目的。
关于c++ - 如果不推荐使用 std::iterator ,我们还应该使用需要 std::iterator 的类或函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65484978/