vectore problems again

Started by
4 comments, last by MaR_dev 16 years, 2 months ago
Why does this not work when the 2nd one does?

	for (std::vector<object*>::iterator it = object_list_depth.end(); it != object_list_depth.begin(); it--)
		if((*it)!=NULL && (*it)->active) (*it)->draw();//Vector iterator is not dereferencable


		for (std::vector<object*>::iterator it = object_list_depth.begin(); it != object_list_depth.end(); it++)
			if((*it)!=NULL && (*it)->active) (*it)->draw();

Advertisement
stl vector's end method returns 1 pass the end of the list, so the first deference isn't actually returning anything in the list, try using:

for (std::vector<object*>::iterator it = object_list_depth.end()-1; it != object_list_depth.begin(); it--)		if((*it)!=NULL && (*it)->active) (*it)->draw();//
...vc\include\vectorLine: 163Expression:("_Myptr + _Off <= ((_Myvec *)(this->_Getmycount()))->_Mylast&&_Myptr+_off >=((_Myvec *)(this->Getmycount()))->_Myfirst",0)

	for (std::vector<object*>::iterator it = object_list_depth.end()-1; it != object_list_depth.begin(); it--)//HERE		if((*it)!=NULL && (*it)->active) (*it)->draw();
Don't.

The operation v.end() - 1 will obviously fail on an empty vector. Instead of making life hard for yourself using normal iterators, use std::vector::rbegin and std::vector::rend.
you could also try using a reverse_iterator ... so:(check that syntax though I don't have an IDE in front of me)

for (std::vector<object*>::reverse_iterator it = object_list_depth.begin(); it != object_list_depth.end(); it++)
if((*it)!=NULL && (*it)->active) (*it)->draw();
for (std::vector<object*>::reverse_iterator it = object_list_depth.rbegin(); it != object_list_depth.rend(); it++)
if((*it)!=NULL && (*it)->active) (*it)->draw();

EDIT: note just fixed RIZAXs example, he forgot to add those 'r's.

[Edited by - MaR_dev on February 22, 2008 6:28:40 AM]

This topic is closed to new replies.

Advertisement