我只想检查迭代器是否指向列表中的对象.
什么是cmd?
谢谢.:)
SkyThe
编辑:
嗯,好吧,我试过了.现在有一个错误:"表达式:列表迭代器不可复制"
也许一些代码:
#include <list>
list<obj> list;
list<obj>::iterator it;
if(it != list.end()){ //here the error pops up when i debug
vShowStatus();
}else{
cout << "...";
}
Run Code Online (Sandbox Code Playgroud)
你不能.没有办法检查.您必须构造代码,以便迭代器位于list.begin()和list.end()之间.
以这种方式使用迭代器:
for (std::list<int>::const_iterator it = myList.begin(); it != myList.end(); ++it)
cout << *it << " ";
Run Code Online (Sandbox Code Playgroud)
你不能将[ EDIT ]列表迭代器与关系运算符(<,>,<=,> =)进行比较,所以当你在for循环之外使用迭代器时,你必须总是通过比较来检查你是否超出了界限. begin()(如果你使用--it向后)或者使用end()(如果你继续使用++).
std::list<int>::const_iterator it = ... // initialized with some CORRECT value
// going backward
while (true)
{
cout << *it;
if (it == list.begin())
break;
--it;
}
// going forward
while (true)
{
cout << *it;
++it;
if (it == list.end())
break;
}
// or simplier
while (it != list.end())
{
cout << *it;
++it;
}
Run Code Online (Sandbox Code Playgroud)
如果由于某种原因,您确实需要检查,那么您可以遍历列表并检查是否有任何迭代器与您的迭代器相同.但这可能会对性能产生相当大的影响,因此仅在调试或/和测试中使用它.
| 归档时间: |
|
| 查看次数: |
2285 次 |
| 最近记录: |