How could I make my own lazy iterator?

廉价感情. 提交于 2019-12-04 03:09:20
ecatmur

Almost; the compiler will look in a few other places to get the begin and end iterators if it can't find begin or end methods on the container class; this is how range-based for loops work on arrays, that don't have begin and end members. It will also look for free functions begin and end by ADL, and eventually std::begin and std::end, so there's plenty of opportunity to retrofit range-based for loop support to existing containers. Section 6.5.4 covers the details.

For your other question, iterators absolutely can be lazy! A good example is std::istream_iterator which has to be lazy as it reads input from the console.

The requirement to use an iterator in a for loop is that it should satisfy the input iterator category, which is described in section 24.2.3; the required operations for that category are !=, unary *, and pre- and post-increment ++.

To let the language know that you've created an input iterator, you should inherit from std::iterator<std::input_iterator_tag, T, void, T *, T &> where T is the type your iterator deals in (section 24.4.3).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!