Jump to content

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


Josh
 Share

Recommended Posts

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.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)?

Link to comment
Share on other sites

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.

 Share

×
×
  • Create New...