Jump to content

Setting a C++ iterator to an "invalid" value?


Recommended Posts

Posted

Is there any way to set an iterator to a state or value that marks it as "invalid"? I end up storing a boolean member along with every iterator like "addedtoshadowcasterupdatelist". It would be a lot easier if I could set set iterator=0 when it is initialized or removed from a list.

Let's build cool stuff and have fun. :)

Posted
It would be a lot easier if I could set set iterator=0 when it is initialized or removed from a list.

 

Are you referring to setting the iterators value = NULL?

 

Maybe something like this?

std::vector<Entity*> entities;
for(auto it = entities.begin(); it != entities.end(); it++)
{
if(condition)
{
 *it = NULL;
}
}

 

Idk if that would work, iterators are just pointers to each element. I'm not too proficient at my C++, more of a C, just a guess.

Posted

He's referring to the iterator, not iterator value.

 

But you can you use the value of the iterator as a flag, although it can be dangerous in case an object or variable does have that value.

An alternative way could perhaps be using it = list.end(), as end() has no value assigned to it.

 

I don't really think there's a clean way to set it to "invalid" in any other way.

Using Leadwerks Professional Edition (Beta), mainly using C++.

Windows 10 / Linux Mint, Visual Studio 2017. GPU: NVidia GeForce GTX970, CPU: Intel i7 7700K @ 4.20 GHz

Previously known as Evayr.

Posted

The problem with the list.end() method is you need a list to compare it to. Okay, create a global one. Now you need one for every single type of list that might exist! :P

 

I have never set the value of an iterator like that. Is that safe and portable?

Let's build cool stuff and have fun. :)

Posted

I've never tried it myself, so use it at your own risk I suppose... :P

Using Leadwerks Professional Edition (Beta), mainly using C++.

Windows 10 / Linux Mint, Visual Studio 2017. GPU: NVidia GeForce GTX970, CPU: Intel i7 7700K @ 4.20 GHz

Previously known as Evayr.

Posted
t would be a lot easier if I could set set iterator=0 when it is initialized or removed from a list.

 

You're storing iterators in a list? What's the goal with this request? Why do you want to do that when it's initialized or removed from a list (still seems odd to store iterators in a list)?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...