473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

maps turn into multimaps

Do maps convert to multimaps if the same key is inserted more than once?
Aug 26 '08 #1
15 2248
On 2008-08-26 21:44, brad wrote:
Do maps convert to multimaps if the same key is inserted more than once?
Of course not, if they did they would be multimaps and not maps.

--
Erik Wikström
Aug 26 '08 #2
Erik Wikström wrote:
On 2008-08-26 21:44, brad wrote:
>Do maps convert to multimaps if the same key is inserted more than once?

Of course not, if they did they would be multimaps and not maps.
I have a map that behaves as if it were a multimap. Two keys are the
same. Compiles and runs as expected.
Aug 26 '08 #3
brad wrote:
Erik Wikström wrote:
>On 2008-08-26 21:44, brad wrote:
>>Do maps convert to multimaps if the same key is inserted more than once?

Of course not, if they did they would be multimaps and not maps.

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.
Aug 26 '08 #4
On 26 Aug., 22:02, brad <byte8b...@gmail.comwrote:
brad wrote:
Erik Wikström wrote:
On 2008-08-26 21:44, brad wrote:
Do maps convert to multimaps if the same key is inserted more than once?
Of course not, if they did they would be multimaps and not maps.
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.
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
Aug 26 '08 #5
On 2008-08-26 22:02, brad wrote:
brad wrote:
>Erik Wikström wrote:
>>On 2008-08-26 21:44, brad wrote:
Do maps convert to multimaps if the same key is inserted more than once?

Of course not, if they did they would be multimaps and not maps.

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.
It is the latter, if you insert a k,v pair and the key already exists it
will be returned (or rather an iterator to it). If you use the
operator[] to insert you will overwrite the existing element.

--
Erik Wikström
Aug 26 '08 #6
peter koch wrote:
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_infocMap;
cMap card_map;

// VISA info
card_info v;
v.card_name = "Visa";
v.card_length = 16;
card_map.insert(std::pair<std::string, card_info>("4", v));

// VISA13 info
card_info v13;
v13.card_name = "Visa";
v13.card_length = 13;
card_map.insert(std::pair<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.
Aug 26 '08 #7
peter koch wrote:
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
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."
Aug 26 '08 #8
On 26 Aug., 22:26, brad <byte8b...@gmail.comwrote:
peter koch wrote:
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_infocMap;
* cMap card_map;

* *// VISA info
* *card_info v;
* *v.card_name = "Visa";
* *v.card_length = 16;
* *card_map.insert(std::pair<std::string, card_info>("4", v));

* *// VISA13 info
* *card_info v13;
* *v13.card_name = "Visa";
* *v13.card_length = 13;
* *card_map.insert(std::pair<std::string, card_info>("4", v13));
You do not validate that the insert succeeds - this is most likey your
problem.

/Peter
Aug 26 '08 #9
peter koch wrote:
You do not validate that the insert succeeds - this is most likey your
problem.

/Peter
Very good... the insert did fail. I'll have to look at this closer. 13
digit cards should not be validating, but they are. There's a bug some
place else (outside of the map code).

Thanks,

Brad

Aug 26 '08 #10
LR
brad wrote:
peter koch wrote:
>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_infocMap;
cMap card_map;

// VISA info
card_info v;
v.card_name = "Visa";
v.card_length = 16;
card_map.insert(std::pair<std::string, card_info>("4", v));

// VISA13 info
card_info v13;
v13.card_name = "Visa";
v13.card_length = 13;
card_map.insert(std::pair<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.
I'm not sure that I follow what you're trying to do here.

Suppose that you have a ctor for card_info,

card_info::card_info(const std::string &s, const int l)
:
card_name(s),
card_length(l)
{}

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?

LR

Aug 26 '08 #11
brad wrote:
peter koch wrote:
>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

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."
Is there even a STL data structure that combines a key and value to form
uniqueness (no, "set" is not)?

Besides, the comparator should be a single parameter template class which
compares keys indicating that the uniqueness is maintained by the keys.
Aug 26 '08 #12
LR wrote:
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 was initially using a map to store k, v pairs about credit cards. I
use the card prefix (4 in the case of visa) as the map key. Most card
prefixes are associated with *one* card length. To my knowledge, visa is
the only major card company with one prefix associated with multiple
card lengths (16 and 13). So credit card pre-validation software needs
to be able to handle this scenario. A map works fine until it is asked
to do this :)

Long story short, I switched to multimap and it now works as expected.
Thanks to all for the tips.
Aug 27 '08 #13
On Aug 27, 8:33 am, brad <byte8b...@gmail.comwrote:
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.
Aug 27 '08 #14
On Aug 27, 4:43*am, Old Wolf <oldw...@inspire.net.nzwrote:
On Aug 27, 8:33 am, brad <byte8b...@gmail.comwrote:
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.
perhaps the adjective unique refers to key, not to key/value pairs.
it's the problem of ambiguous grammars.
Aug 27 '08 #15
On 2008-08-27 05:51:00 -0400, juanvicfer <ju********@gmail.comsaid:
On Aug 27, 4:43Â*am, Old Wolf <oldw...@inspire.net.nzwrote:
>On Aug 27, 8:33 am, brad <byte8b...@gmail.comwrote:
>>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.

perhaps the adjective unique refers to key, not to key/value pairs.
it's the problem of ambiguous grammars.
If that was the intention, the problem is sloppy writing.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 27 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jeff Sandys | last post by:
I'm trying to write a mapping function for genealogy. I want to read a gedcom database and plot an icon at a geographic location based on a user's query. Can you help me find: 1) A python...
4
by: Stanimir Stamenkov | last post by:
I have this kind of construct: http://www.geocities.com/stanio/temp/usemap01.html (an IMG element using MAP described with AREA elements) I'm trying to make it more accessible and currently...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
8
by: Safalra | last post by:
I'm planning to put some star maps on my website. Each constellation (above about 40 degrees south) will have a page, starting with a star map and followed by details of the mjor objects. I'm...
3
by: Sean | last post by:
Have you ever wanted to add the great features inherent in Google Maps? Here is how you do it. ============== == STEP ONE == ============== Create a new MS Access form called frmGoogleMap....
3
by: jshanman | last post by:
I invite the members and viewers of this group to try my Real Time Strategy Multiplayer game created using the Google Maps API. This has not been announced until now and I am looking for...
6
by: BJörn Lindqvist | last post by:
Hello, I'm looking for an algorithm to project "MUD maps" such as the following map: http://www.aww-mud.org/maps/MUD_Maps/Caerin-colour.jpg MUD:s consists of rooms, each rooms has up to four...
6
by: Gumbatman | last post by:
Is there a way to use the Google Maps API in C-Sharp? I've found very little when seaching the web. I don't know JavaScript very well and I thought doing it in C-Sharp would be easier. Thank...
2
by: Nikhil.S.Ketkar | last post by:
Hi, How does the == operator for multimap in STL behave ? I was under the impression that this is supposed to properly compare multimaps for equality. It seems that what it actually does is just...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.