B
brad
Do maps convert to multimaps if the same key is inserted more than once?
Do maps convert to multimaps if the same key is inserted more than once?
Erik said:Of course not, if they did they would be multimaps and not maps.
brad said:I have a map that behaves as if it were a multimap. Two keys are the
same. Compiles and runs as expected.
Wait... perhaps I misundersatnd maps. Is it unique k,v pairs or just
unique keys? If it's the former, then that would make sense. I was under
the impression it was the latter.
Wait... perhaps I misundersatnd maps. Is it unique k,v pairs or just
unique keys? If it's the former, then that would make sense. I was under
the impression it was the latter.
peter said:It is unique key, of course. You most likely have a bug in the
comparison function. Show some code if you want more help.
/Peter
peter said:It is unique key, of course. You most likely have a bug in the
comparison function. Show some code if you want more help.
/Peter
It's a test for credit card pre-validation. I added 13 digit visa cards
to the test in addition to the more common 16 digit visa. The prefix for
all visas is "4" (which I use as the key). See below. As I said, it
works, I was just trying to better understand *why* it works
struct card_info
{
std::string card_name;
int card_length;
};
typedef std::map<std::string, card_info> cMap;
cMap card_map;
// VISA info
card_info v;
v.card_name = "Visa";
v.card_length = 16;
card_map.insert(std:air<std::string, card_info>("4", v));
// VISA13 info
card_info v13;
v13.card_name = "Visa";
v13.card_length = 13;
card_map.insert(std:air<std::string, card_info>("4", v13));
peter said:You do not validate that the insert succeeds - this is most likey your
problem.
/Peter
brad said:It's a test for credit card pre-validation. I added 13 digit visa cards
to the test in addition to the more common 16 digit visa. The prefix for
all visas is "4" (which I use as the key). See below. As I said, it
works, I was just trying to better understand *why* it works
struct card_info
{
std::string card_name;
int card_length;
};
typedef std::map<std::string, card_info> cMap;
cMap card_map;
// VISA info
card_info v;
v.card_name = "Visa";
v.card_length = 16;
card_map.insert(std:air<std::string, card_info>("4", v));
// VISA13 info
card_info v13;
v13.card_name = "Visa";
v13.card_length = 13;
card_map.insert(std:air<std::string, card_info>("4", v13));
This insert works at compile time and there are no erros at run time, I
can pre-validate either 16 or 13 digit visa cards based on the above info.
brad said:I might also note that this reference:
http://www.cppreference.com/cppmap/index.html
Indicates unique "key/value pairs". I was surprised to read that. It's
incorrect... if indeed we are talking about unique keys:
"C++ Maps are sorted associative containers that contain unique
key/value pairs."
LR said:and then I think your code above is the equivalence of:
card_map["4"] = card_info("Visa",16);
card_map["4"] = card_info("Visa",13);
This ends up with card_map having one entry with
key == std::string("4") and value == card_info("Visa",13)
Is this what you want? If so, then why bother with the first insertion?
Did I misunderstand?
I might also note that this reference:
http://www.cppreference.com/cppmap/index.html
Indicates unique "key/value pairs". I was surprised to read that. It's
incorrect... if indeed we are talking about unique keys:
cppreference.com is well known for containing
completely wrong statements like this. I'd
avoid the site entirely if I were you.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.