Jump to content
  • Advertisement
Sign in to follow this  
Sfpiano

Inserting data into a map

This topic is 4967 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

Here's how I'm currently putting data into my map:
std::map<char*, Edge> edgeMap;
...
edgeMap.insert( std::map<char*, Edge>::value_type(data, edge) );
The problem is no matter how many times I call the insert function it always tells me that the size of my map is one, so I have no way of iterating all the way through it.

Share this post


Link to post
Share on other sites
Advertisement
you need the std::make_pair function.

edgeMap.insert( std::make_pair(data, edge) );

EDIT: actually, now that i read your question, im not sure i understand what the problem is. so it IS compiling? either way, try that out. ive never inserted into a map without using make_pair, so im not sure if your method is working properly or not.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sfpiano
Here's how I'm currently putting data into my map:

std::map<char*, Edge> edgeMap;
...
edgeMap.insert( std::map<char*, Edge>::value_type(data, edge) );


The problem is no matter how many times I call the insert function it always tells me that the size of my map is one, so I have no way of iterating all the way through it.


I use the [] and = for inserting into a map.
edgeMap[data] = edge;

--- the key (data) must be different or else it will overwrite the existing one.
insert function will return a pair with an iterator, and a bool (whether it inserted or not). Maybe you should check the bool to see if it succeeded in inserting or not.

Share this post


Link to post
Share on other sites
Yes my program compiles, but no matter how many items I insert into the map it always tells me the size of the map is only one, so I have no way of iterating through the map. I tried all three ways and all three give the same result.


edgeMap.insert(std::make_pair(data, edge));
or
edgeMap.insert( std::map<char*, Edge>::value_type(data, edge) );
or
edge[data] = edge;

int ctr=0;
std::map<char*, Edge>::iterator i;
for( i=edgeMap.begin(); i!=edgeMap.end(); i++ ) {
ctr++;
}


When I run that ctr is always equal to one, and data is different everytime I insert something.

Share this post


Link to post
Share on other sites
I don't see how it could be going out of scope

function(int to, int from, int flag) {
char data[1024];
sprintf(data, "%d_%d", to, from);
Edge edge(to, from, flag);
edgeMap.insert(std::make_pair(data, edge));
}


Apparently everytime I insert something it always puts it into the first spot, because everytime I run the function, the map.begin() iterator is always changing.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sfpiano
Apparently everytime I insert something it always puts it into the first spot, because everytime I run the function, the map.begin() iterator is always changing.


That's because the data you put in the pair is the address of the array, not its content. And that address, not the contents of the array, is what the map uses as the key (i.e. it will do pointer comparisons, not string comparisons). Until this address changes (i.e. the structure of your call stack changes so that the function's local variables are at a different address) they will all be treated as the same key. (And as gfilla pointed out, the array itself goes out of scope when the function returns).

A std::map<char*,whatever> pretty much mandates that you dynamically allocate your char* keys (which makes the clean-up a pain), and provide a custom comparison function (otherwise you're just comparing pointers).

Use a std::map<std::string, whatever> instead.

Share this post


Link to post
Share on other sites
Then it tells me:
error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator

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!