同时遍历两个std :: lists [英] iterate through two std::lists simultaneously
本文介绍了同时遍历两个std :: lists的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
很抱歉,如果这个问题太简单了.
Sorry if this is too simple a question.
事先进行错误检查可确保l1.size() == l2.size()
.
Prior error checking ensures l1.size() == l2.size()
.
std::list<object1>::iterator it1 = l1.begin();
std::list<object2>::iterator it2 = l2.begin();
while(it1 != l1.end() && it2 != l2.end()){
//run some code
it1++;
it2++;
}
这是一种合理的方法,还是有一个更优雅的解决方案?感谢您的帮助.
Is this a reasonable approach, or is there a more elegant solution? Thanks for your help.
推荐答案
如果无条件发生增量,我更喜欢使用for
:
I prefer to use for
if increments unconditionally occurs:
for(; it1 != l1.end() && it2 != l2.end(); ++it1, ++it2)
{
//run some code
}
在列表大小相同的情况下,您可以省略一项测试,但是我不确定运行一些代码中发生了什么!
You can omit one test while the size of lists are the same, but I'm not sure what's going on in run some code!
这篇关于同时遍历两个std :: lists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文