PDA

View Full Version : c++ - do I have to use iterators?


_Dan
11-03-2005, 09:10 PM
Hi. I have some bugs in my program, and just wondered if it was because of me not knowing enough about vectors.

I need to loop through a vector...


std::vector <LInode *>::iterator i;
for(i = node[n]->child.begin(); i != node[n]->child.end(); i++)
//access with *i


or


for (LIuint i = 0; i < node[n]->child.size(); i ++)
//acess with node[n]->child[i]


Does it matter which method I use?

Thanks.:)

aman
11-04-2005, 10:43 PM
You can normally access the vector either way, that's why the vector class has an overloaded subscript [] operator.


The safest way is to use the iterator, that's what it's there for. Otherwise, for testing you should use the vector::At() member function because the subscript operator doesn't perform any bounds checking.

_Dan
11-04-2005, 11:01 PM
Good good. I have tracked down the bug, it was elsewhere in the program, and now either way does seem to work. Thanks.