STL map problem! function find()

Good morning, I have a problem when dealing with the map stl library, and It would be great if anyone could help me solve it.

I have this code:
1
2
3
4
5
6
7
public:
    typedef person* key_person;    
    typedef std::map<  person*, int > friends; //map named " friends" which contains a person and an int. 
private:
    typedef std::map<key_person,friends> graph_data; //map that contains a key_person and another map.
public:
    graph_data data;

Where person is a class.

And then Lets say that I wanna add to "data" a person object name lucy, but without any map friends, I just wanna add lucy to the "data" map , just the person and not another map, remember that data is like this:
map<key_person,friends> where friends is another map.


person* lucy=new person();
data.insert(lucy);

But Im getting an error:
error: no matching function for call to 'std::map<person*, std::map<person*, int> >::insert(person*)'|



I dont wanna add any map friends yet, but i will.
I would really appreciate any given help!
Last edited on
when you insert into a map you need to insert a pair
so insert a pair
 
data.insert( make_pair( lucy, friends() ) );


friends() is just a blank map
as you said
but why do you need to do that
you can just insert it later on
Last edited on
With regards to this part of your code:

1
2
person* lucy=new person();
data.insert(lucy);


Be advised that in C++, every new must have a corresponding delete or the program will leak memory.

Perhaps you should rethink your design: is storing pointers to dynamically allocated memory what you really want?
Oh thanks! I got it! I forgot the new
Thanks both of you

Then, if later on I want to insert to lucy a map as the value?
how do i do?
Last edited on
lucy as the value what do you mean ?
the value is an int what value are you talking about

if you want to add value to the map do this
1
2
3
person *bob = new person();
person *lucy = new person();
data[lucy][bob] = 5;



and I agree with CatFish4
it's strange in the way you store pointer as a key value
I mean where would you delete it when you don't need it anymore...
Last edited on
shouldnt I iterate through the map until I find what I wanna delete and make a delete?

I was asked to do a graph of people with maps, where an arc of a vertex represents a friends of the person.

Sorry for not making myself clear, I want this:

1
2
3
friends fr;
fr.insert(make_pair( adam, 1));
fr.insert(make_pair(joel,1));

What I wanna do is add fr to the key lucy, remember you suggested this:

data.insert( make_pair( lucy, friends() ) )
Now I wanna add fr as the value of the key lucy.

Sorry again if I dont make myself clear
Last edited on
no you shouldn't iterate trough it
because map can do binary search which is far better than a linear search
If you want to delete lucy then
 
data.erase( lucy );

map can delete based on key


if you want to add fr to let's say lucy
1
2
3
4
auto ptr = data.find( lucy );
if( ptr != data.end() ){
    ptr->second.insert( fr.begin(), fr.end() );
}


if let's say adam has existed inside data[lucy] then it won't be replaced
Last edited on
Topic archived. No new replies allowed.