473,569 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dictionary and std::multimap

Hello, Newsgroupians:

I've a question regarding dictionaries. I have an array elements that I
created, and I'm trying to sort those elements into various sections. Here's
the template of my data type.

DT
{
int nSize;
...
}

I'm trying to create a dictionary, which resembles a jagged array, but I
can't figure but how to do this. I'm trying to sort it according to the
nSize.

EXA: Here's a jagged-array of the structure. nSize is the key, which is
before the colon.

1: Car1, Car2, Car3
3: Car4
8: Car5, Car6

From the example, I have three sizes -- but this, of course can vary. How
can I create a dictionary that would allow me to mimic the std::multimap<> ?
I've been trying Dictionary<int, List<DT>>, but I can't figure out how to add
or access the dictionary. One thing I would like to do is determine the
number of keys in the dictionary, which I can do by dictionary.Key. Count, but
then how can I get a count of all the values associated with a specific key?

Thank you, all.
Trecius
Jun 27 '08 #1
18 4277
On Mon, 21 Apr 2008 12:05:00 -0700, Trecius
<Tr*****@discus sions.microsoft .comwrote:
[...]
From the example, I have three sizes -- but this, of course can vary.
How
can I create a dictionary that would allow me to mimic the
std::multimap<> ?
I don't know anything about std::multimap<s pecifically, but...
I've been trying Dictionary<int, List<DT>>, but I can't figure out how
to add
or access the dictionary.
To add:

Assuming:

Dictionary<int, List<DT>dict;
DT dtAdd;

Then:

if (dict.Contains( dtAdd.nSize))
{
dict[dtAdd.nSize].Add(dtAdd);
}
else
{
List<DTlist = new List<DT>();

list.Add(dtAdd) ;
dict.Add(dtAdd. nSize, list);
}

I'm not really sure what you mean by "access", but the above code includes
an example of how to actually access an element in the Dictionary<
instance.
One thing I would like to do is determine the
number of keys in the dictionary, which I can do by
dictionary.Key. Count, but
then how can I get a count of all the values associated with a specific
key?
For example, where "nSize" is a specific key:

dict[nSize].Count;

Hope that helps.

Pete
Jun 27 '08 #2
Thank you, Mr. Duniho. That is exactly what I'm looking for. Thank you again.
Trecius

"Peter Duniho" wrote:
On Mon, 21 Apr 2008 12:05:00 -0700, Trecius
<Tr*****@discus sions.microsoft .comwrote:
[...]
From the example, I have three sizes -- but this, of course can vary.
How
can I create a dictionary that would allow me to mimic the
std::multimap<> ?

I don't know anything about std::multimap<s pecifically, but...
I've been trying Dictionary<int, List<DT>>, but I can't figure out how
to add
or access the dictionary.

To add:

Assuming:

Dictionary<int, List<DT>dict;
DT dtAdd;

Then:

if (dict.Contains( dtAdd.nSize))
{
dict[dtAdd.nSize].Add(dtAdd);
}
else
{
List<DTlist = new List<DT>();

list.Add(dtAdd) ;
dict.Add(dtAdd. nSize, list);
}

I'm not really sure what you mean by "access", but the above code includes
an example of how to actually access an element in the Dictionary<>
instance.
One thing I would like to do is determine the
number of keys in the dictionary, which I can do by
dictionary.Key. Count, but
then how can I get a count of all the values associated with a specific
key?

For example, where "nSize" is a specific key:

dict[nSize].Count;

Hope that helps.

Pete
Jun 27 '08 #3
Peter Duniho wrote:
On Mon, 21 Apr 2008 12:05:00 -0700, Trecius
<Tr*****@discus sions.microsoft .comwrote:
>[...]
From the example, I have three sizes -- but this, of course can vary.
How
can I create a dictionary that would allow me to mimic the
std::multimap< >?

I don't know anything about std::multimap<s pecifically, but...
>I've been trying Dictionary<int, List<DT>>, but I can't figure out
how to add
or access the dictionary.

To add:

Assuming:

Dictionary<int, List<DT>dict;
DT dtAdd;

Then:

if (dict.Contains( dtAdd.nSize))
{
dict[dtAdd.nSize].Add(dtAdd);
}
else
{
List<DTlist = new List<DT>();

list.Add(dtAdd) ;
dict.Add(dtAdd. nSize, list);
}
A little less redundant code:

List<DTmulti;

if (!dict.TryGetVa lue(dtAdd.nSize , out multi))
dict.Add(dt.nSi ze, multi = new List<DT>());

multi.Add(dtAdd );
>
I'm not really sure what you mean by "access", but the above code
includes an example of how to actually access an element in the
Dictionary<inst ance.
>One thing I would like to do is determine the
number of keys in the dictionary, which I can do by
dictionary.Key .Count, but
then how can I get a count of all the values associated with a
specific key?

For example, where "nSize" is a specific key:

dict[nSize].Count;

Hope that helps.

Pete

Jun 27 '08 #4
On Mon, 21 Apr 2008 14:19:22 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nos pamwrote:
A little less redundant code:

List<DTmulti;

if (!dict.TryGetVa lue(dtAdd.nSize , out multi))
dict.Add(dt.nSi ze, multi = new List<DT>());

multi.Add(dtAdd );
Not that this would normally matter, but...in a previous test I found that
TryGetValue() was slower than calling Contains() followed by an access of
the specific element.

I never did figure out why that was, and it seems counter-intuitive to
me. But that's what I found.

Because of that, I tend to use Contains() rather than TryGetValue(). The
version of the above code with TryGetValue() only removes a single call to
Add(), at the expense of being slightly harder to read. Even if it
performed better, the reduced obviousness of the code would IMHO argue in
favor of the alternative, but being harder to read _and_ performing more
poorly is a pretty negative combination, even if hte differences are
slight.

YMMV. People who are really more C++ programmers than C# programmers have
a tendency to love highly-compressed code. There's a sort of "let's see
how much functionality we can squeeze into a single line" mentality going
on. :) Personally, I don't mind my code to be spread out a bit,
especially when it's easier to see what's going on.

Pete
Jun 27 '08 #5
Not that this would normally matter, but...in a previous test I found
that TryGetValue() was slower than calling Contains() followed by an
access of the specific element.

I never did figure out why that was, and it seems counter-intuitive to
me. But that's what I found.

Because of that, I tend to use Contains() rather than TryGetValue(). The
version of the above code with TryGetValue() only removes a
single call to Add(), at the expense of being slightly harder to
read. Even if it performed better, the reduced obviousness of the
code would IMHO argue in favor of the alternative, but being harder
to read _and_ performing more poorly is a pretty negative
combination, even if hte differences are slight.
Ok, you could use Contains and then either add a new element or retrieve the
existing one. You could even make a class implementing IDictionary that
auto-instantiates missing members and hides the details of how it does so.
>
YMMV. People who are really more C++ programmers than C# programmers
have a tendency to love highly-compressed code. There's a sort of
"let's see how much functionality we can squeeze into a single line"
mentality going on. :) Personally, I don't mind my code to be
spread out a bit, especially when it's easier to see what's going on.
Well, I wasn't actually trying to make the code denser.

I was trying to first establish an invariant (dict contains an element for
dtAdd.nSize).

Once the invariant is established you no longer need two versions of the
Add-to-list code that need to be maintained (ok, now it's a single function
call but it might not stay that way).
>
Pete

Jun 27 '08 #6
Not that this would normally matter, but...in a previous test I found
that TryGetValue() was slower than calling Contains() followed by an
access of the specific element.

I never did figure out why that was, and it seems counter-intuitive to
me. But that's what I found.
I'd like to see that benchmark... because disassembling the code there is no
way that can be true, unless TryGetValue causes a performance bug in the
JIT.

Also Jon has a very relevant blog entry here:
http://msmvps.com/blogs/jon.skeet/ar...tiful-one.aspx
Jun 27 '08 #7
Shame I didn't spot this thread earlier... But!

..NET 3.5 intruduces ILookup<TKey,TV aluewhich is comparable to a
multimap; the default implementation (Lookup<TKey,TV alue>) is immutable,
but it is trivial to write a mutable implementation; in fact, one is in
MiscUtil - EditableLookup< TKey,TValue>:

http://www.pobox.com/~skeet/csharp/miscutil/

And you can use it in 2.0 as well (the MiscUtil build stubs-out the
missing interfaces down in the 2.0 configuration).

Marc
Jun 27 '08 #8
On Tue, 22 Apr 2008 06:52:13 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nos pamwrote:
>Not that this would normally matter, but...in a previous test I found
that TryGetValue() was slower than calling Contains() followed by an
access of the specific element.

I never did figure out why that was, and it seems counter-intuitive to
me. But that's what I found.

I'd like to see that benchmark... because disassembling the code there
is no
way that can be true, unless TryGetValue causes a performance bug in the
JIT.
Nothing would make me happier than to have another pair of eyes look at
the benchmark and explain my observations (if only to show that I did
something wrong in timing the code).

I don't have handy access to the code right now, but I'll post when I do.
Fair warning: I don't have time to clean it up and it was written in
pursuit of a different question. (Here's the context:
http://groups.google.com/group/micro...c79d93deffc692)

Pete
Jun 27 '08 #9
On Tue, 22 Apr 2008 17:29:32 -0700, Willy Denoyette [MVP]
<wi************ *@telenet.bewro te:
Hmmm... as expected, there is no clear winner.
Well, actually my expectation isn't that there's no clear winner, but
rather than the TryGetValue() version is consistently faster.

What I don't understand is why, at least on my computer (apparently not
yours), Contains() is consistently slower. These tests are very
repeatable, and while the variance is definitely higher on my computer
than yours, the slowest time for Contains() is only slower than one test
of TryGetValue(), and frankly that's only because it's the very first test
(which in my tests has consistently always been very slow...slow enough
that I think it'd actually be more fair to run a couple of throw-away
tests and then run five of each throwing out the high and low).

Speaking of that slow first test, I'll point out that even on your
computer, if you toss out the first run, Contains() is faster than
TryGetValue() for all but one other test. I'm not really convinced that
your tests show "no clear winner". I'd be more willing to accept that
statement if you presented a run that includes a couple of throw-away
tests and still showed a random distribution across both tests. Given the
apparently closer results on your computer, doing that is much more
important than it would be for my tests.

But in any case, I'm still without an explanation as to why there should
be such a clear difference on my computer.

Pete
Jun 27 '08 #10

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

Similar topics

12
13434
by: Tanguy Fautré | last post by:
Hello, does std::multimap make any guarantee about the insertion order? for example: int main() { std::multimap<int, int> Map;
9
7855
by: Dennis Jones | last post by:
Hi, Is there a way to iterate through a multimap in such a way as to encounter only the unique keys? In other words, since a multimap allows duplicate keys, I would like to iterate through the entire multimap, and if a particular key has duplicates, only see one of them. I don't think I care about which of the duplicate entries I see,...
1
3789
by: melfar | last post by:
Hello. 9 says: '' The fundamental property of iterators of associative containers is that they iterate through the containers in the non-descending order of keys where non-descending is defined by the comparison that was used to construct them. '' Does it mean that if I insert values into a multimap with equal keys, I
1
1598
by: SpreadTooThin | last post by:
I have a dictionary that looks like: a0,b0,c0,d0,e0 a1,b1,c1,d1,e1 .... .... Currently I consider (Class Tag) to be the key and (Class Value) to be the value. However I now need to use as the key and as the value.
20
3959
by: puzzlecracker | last post by:
I am using while loop for that but I am sure you can do it quicker and more syntactically clear with copy function. Here is what I do and would like to if someone has a cleaner solution: vector<stringvec; multimap<stirng, intmyMap // populate myMap
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.