Jump to content
  • Advertisement
Sign in to follow this  
baker

map of objects question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, I have a map that is made up of INT and an object. the map is declared as map<int, objint const *>. after i find the 'map' object that i need via the c1.find(3) for example, how do i update the object? int main() { objint *testptr = new objint(); testptr->numobj = "2"; map<int, objint const *> c1; map<int, objint const * >::iterator pos; c1.insert(map<int, objint const *>::value_type(5,testptr)); c1.insert(map<int, objint const *>::value_type(3,testptr)); c1.insert(map<int, objint const *>::value_type(2,testptr)); pos = c1.find(3); //so now my iterator is pointing to the object with ID of 3. I want to now //update whats in that object. i tried creating a pointer to point to what pos //is pointing to, but i dont think that will work. anyone care to share? thanks! return 0; }

Share this post


Link to post
Share on other sites
Advertisement
I'm confused about your insertion procedure. Is there a good reason you aren't doing


c1[5] = testptr;
c1[3] = testptr;
c1[2] = testptr;


?

Anyhow, the key is pos->first whilst the value is pos->second.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Since a map is made up of two items, the first being the key, the second being the value, the easiest way while using an interator is as follows:

pos->second->update(); //if update was the function you wanted to call

or

pos->second //access just the object you created

Share this post


Link to post
Share on other sites
hi nathan, thanks for the reply. i wasnt sure how to insert new items into the map. i just saw how someone did it in the book im reading.


ok, so now i can insert, but is it possible to update the object that testptr is now pointing to? like can testptr call a function of the object?

for example my objint has a field called num_objects. is there a way to find what i want in the map so i can update testptr->num_objects = 100; ??

Share this post


Link to post
Share on other sites
awesome. thanks guys!.


so in the map class, the iterator has access to a first and second pointer which can be used to modify everything within the map object.

awesome!

Share this post


Link to post
Share on other sites
Also, you might look into the [] operator for std::map<>; it's a nice shortcut for the more verbose insert() syntax, and will clean up your code considerably.

Share this post


Link to post
Share on other sites
Quote:
Original post by baker
hi nathan, thanks for the reply. i wasnt sure how to insert new items into the map. i just saw how someone did it in the book im reading.


ok, so now i can insert, but is it possible to update the object that testptr is now pointing to? like can testptr call a function of the object?

for example my objint has a field called num_objects. is there a way to find what i want in the map so i can update testptr->num_objects = 100; ??


Make the map have non const pointers, like so:


int main()
{
objint *testptr = new objint();

testptr->numobj = "2";

map<int, objint*> c1;
map<int, objint* >::iterator pos;


c1.insert(map<int, objint*>::value_type(5,testptr));
c1.insert(map<int, objint*>::value_type(3,testptr));
c1.insert(map<int, objint*>::value_type(2,testptr));

pos = c1.find(3);

// test whether we found it.
// we know we will here but for future reference
if( pos != c1.end() )
{
objint *ptr = pos->second;
ptr->num_objects = 100; // set a field
ptr->doStuff(); // call a method
}


return 0;
}

Share this post


Link to post
Share on other sites
thanks some more for the replies. 1 more question,

if maps have a first and second pointer, what do i use to call an objects function from a vector?

Share this post


Link to post
Share on other sites
vector<myobj *> c1;
vector<myobj *>::iterator pos;

myobj *ptr = new myobj();
ptr->setval(19);

c1.push_back(ptr);
c1.push_back(ptr);
c1.push_back(ptr);
c1.push_back(ptr);

pos = c1.begin();


now how can i call an objects function that testptr is pointing to in the first vector location?

thanks







Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!