c++ - 如果不推荐使用 std::iterator ,我们还应该使用需要 std::iterator 的类或函数吗?

标签 c++ for-loop iterator c++17

这个问题在这里已经有了答案:





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 from std::iterator?


标准库迭代器类型由标准库实现定义。从技术上讲,它们可以继承自 std::iterator但它们不是必需的,也不是保证从它继承的。他们是否继承自std::iterator与否,它们本身并没有被弃用。
std::iterator过去打算用作自定义迭代器的基类。除非您正在定义自定义迭代器,否则您一开始就不会使用它。如果您正在定义自定义迭代器,则不应使用 std::iterator了。请注意,从来没有必要使用 std::iterator以此目的。

关于c++ - 如果不推荐使用 std::iterator ,我们还应该使用需要 std::iterator 的类或函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65484978/

相关文章:

c++ - 如何在 Win32 项目、Visual Studio 2010 中添加控制台窗口

c++ - 在作为引用传递的参数中创建对象

java - 使用 for 循环仅读取 5 个数字并对它们求平均值。 java

javascript - style.display 为 null - 尝试遍历数组并更改显示属性

python - iter,for循环python中的范围

scala - Scala 中的惰性树遍历迭代器

c++ - 动态内存 C++

Java循环效率

c++ - std::copy 如何与流迭代器一起工作

c++ - equal_range 和 std::unordered_multiset