C++ nested iterators

安稳与你 提交于 2019-12-06 09:40:16
Rafał Rawicki

Your code is correct.

Both iterators will have the same order and incrementing j doesn't affect i as long as you don't make any operation that invalidates iterators (for example erasing from or pushing to vector).

This is absolutely fine as long as you don't do anything inside the loops that might invalidate iterators.

(As an aside, list is a questionable name for a std::vector in my opinion.)

That's perfectly fine. Random access doesn't mean random order. It means that you can jump through the container by using additive operators (+ and -) on your iterator. For example, with a random access iterator it, you can do it + 10. For an non-random access iterator, you would have to do it++ 10 times to achieve the same effect. (the std::advance function will encapsulate this for you though)

This should work fine. Vector stores elements in order, and both iterators will obey this order.

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