Public Group

# vectore problems again

This topic is 3773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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();



##### Share on other sites
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();//

##### Share on other sites
...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();

##### Share on other sites
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.

##### Share on other sites
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();

##### Share on other sites
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]

• 11
• 20
• 12
• 9
• 34
• ### Forum Statistics

• Total Topics
631399
• Total Posts
2999846
×