C ++嵌套迭代器 [英] C++ nested iterators

查看:170
本文介绍了C ++嵌套迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用如下的嵌套迭代器?

Is it OK to have a nested iterator like the following?

for (vector<type>::iterator i = list.begin(); i != list.end(); ++i) {
    for (vector<type>::iterator j = i; j != list.end(); ++j) {
        ...
    }
}

请注意 j i 开始,而不是 list.begin() 。由于迭代器是随机访问,我可以保证 i j 将具有相同的顺序吗?有更好的方法吗?

Note that j starts at i, and not list.begin(). Since the iterator is random access, can I guarantee that both i and j will have the same order? is there a better way of doing this?

推荐答案

您的代码是正确的。

两个迭代器将具有相同的顺序并且递增 j 不会影响 i 只要你不要不要进行 使迭代器无效 的任何操作(例如从矢量中删除或推送到矢量)。

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).

这篇关于C ++嵌套迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆