map of objects question
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;
}
I'm confused about your insertion procedure. Is there a good reason you aren't doing
?
Anyhow, the key is pos->first whilst the value is pos->second.
c1[5] = testptr;c1[3] = testptr;c1[2] = testptr;
?
Anyhow, the key is pos->first whilst the value is pos->second.
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
pos->second->update(); //if update was the function you wanted to call
or
pos->second //access just the object you created
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; ??
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; ??
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!
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!
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.
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 referenceif( pos != c1.end() ){ objint *ptr = pos->second; ptr->num_objects = 100; // set a field ptr->doStuff(); // call a method}return 0;}
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?
if maps have a first and second pointer, what do i use to call an objects function from a vector?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement