Sign in to follow this  

map of objects question

This topic is 4301 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
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
Quote:
Original post by baker
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


Just like any array:



c1[0]->someMethod();

// or, call it on every object

for( int i = 0 ; i < c1.size() ; ++i )
{
c1[i]->someOtherMethod();
}






Or using your iterator:



(*pos)->someMethod();

for( pos = c1.bein() ; pos != c1.end() ; ++pos )
{
(*pos)->someOtherMethod();
}






Edit: ToohrVyk is right, your need (*iterator)-> for a pointer...

Share this post


Link to post
Share on other sites
ok so when i am using vector the pointer must be in between ( ) with the star?

myptr->somefunction is not the same as (*myptr)->somefunction

i just tried it out so i guess the second is the way to go?

Share this post


Link to post
Share on other sites

This topic is 4301 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this