469,569 Members | 1,566 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,569 developers. It's quick & easy.

C# version of std::map

Hi

Can anyone tell me if there is a class in C# similar to the class std::map
in C++?

I need a way to map enum to double and was hoping to avoid using any "if"
code
Thanks Torben
Apr 18 '06 #1
7 19618
"Torben Laursen" <To****@newsgroups.nospam> a écrit dans le message de news:
Od****************@TK2MSFTNGP03.phx.gbl...

| Can anyone tell me if there is a class in C# similar to the class std::map
| in C++?
|
| I need a way to map enum to double and was hoping to avoid using any "if"
| code

System.Collections.Hashtable in .NET 1.1

System.Collections.Generic.Dictionary<k,v> in .NET 2.0

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Apr 18 '06 #2
Thanks
I looked at that but I need a way to get a 2D map.
This is my C++ code:
enum Units {Pa=0, Bar, Atm, kPa, Psi, MPa};
std::map<Units, std::map<Units, double> > P2P_;

Then I can use P2P_ as: P2P_[Pa][Bar] where it returns a double

It there a way to do this in C#?

Thanks Torben

"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:ua**************@TK2MSFTNGP03.phx.gbl...
"Torben Laursen" <To****@newsgroups.nospam> a écrit dans le message de
news:
Od****************@TK2MSFTNGP03.phx.gbl...

| Can anyone tell me if there is a class in C# similar to the class
std::map
| in C++?
|
| I need a way to map enum to double and was hoping to avoid using any
"if"
| code

System.Collections.Hashtable in .NET 1.1

System.Collections.Generic.Dictionary<k,v> in .NET 2.0

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Apr 18 '06 #3
"Torben Laursen" <To****@newsgroups.nospam> a écrit dans le message de news:
e9**************@TK2MSFTNGP04.phx.gbl...

| I looked at that but I need a way to get a 2D map.
| This is my C++ code:
| enum Units {Pa=0, Bar, Atm, kPa, Psi, MPa};
| std::map<Units, std::map<Units, double> > P2P_;
|
| Then I can use P2P_ as: P2P_[Pa][Bar] where it returns a double
|
| It there a way to do this in C#?

How about Dictionary<Units, Dictionary<Units, double>> ??

public enum Units
{
Pa = 0, Bar, Atm, kPa, Psi, MPa
}

class Program
{
static void Main(string[] args)
{
Dictionary<Units, Dictionary<Units, double>> p2p = new
Dictionary<Units,Dictionary<Units,double>>();

Dictionary<Units, double> newEntry = new Dictionary<Units,double>();

newEntry.Add(Units.Atm, 12.34);

p2p.Add(Units.Pa, newEntry);

double result = p2p[Units.Pa][Units.Atm];

System.Console.WriteLine(result);

System.Console.ReadLine();
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Apr 18 '06 #4

Joanna Carter [TeamB] wrote:
"Torben Laursen" <To****@newsgroups.nospam> a écrit dans le message de news:
Od****************@TK2MSFTNGP03.phx.gbl...

| Can anyone tell me if there is a class in C# similar to the class std::map
| in C++?
|
| I need a way to map enum to double and was hoping to avoid using any "if"
| code

System.Collections.Hashtable in .NET 1.1

System.Collections.Generic.Dictionary<k,v> in .NET 2.0


Although the interface is similar, you need to be aware that the
implementation is quite different, and so will be the performances :
std::map is sorted according to the key whereas HashTable and
Dictionnary are hash-based collections. The complexity of the various
operations on the container are therefore quite different, as are the
operations that are authorized on the container (for example, .NET
forbids to modify in any way the container's content during an
enumeration on it).

Arnaud
MVP - VC

Apr 18 '06 #5
ad******@club-internet.fr wrote:
Although the interface is similar, you need to be aware that the
implementation is quite different, and so will be the performances :
std::map is sorted according to the key whereas HashTable and
Dictionnary are hash-based collections.


SortedList<Tkey,Tvalue> and SortedDictionary<Tkey,Tvalue> are the two
key-sorted dictionaries available in c#. I never remember what the
difference is.

--
Lucian
Apr 19 '06 #6
Lucian Wischik <lu***@wischik.com> wrote:
ad******@club-internet.fr wrote:
Although the interface is similar, you need to be aware that the
implementation is quite different, and so will be the performances :
std::map is sorted according to the key whereas HashTable and
Dictionnary are hash-based collections.


SortedList<Tkey,Tvalue> and SortedDictionary<Tkey,Tvalue> are the two
key-sorted dictionaries available in c#. I never remember what the
difference is.


From the docs (I didn't know either):

<quote>
Where the two classes differ is in memory use and speed of insertion
and removal:

SortedList uses less memory than SortedDictionary.

SortedDictionary has faster insertion and removal operations for
unsorted data, O(log n) as opposed to O(n) for SortedList.

If the list is populated all at once from sorted data, SortedList is
faster than SortedDictionary.

Another difference between the SortedDictionary and SortedList classes
is that SortedList supports efficient indexed retrieval of keys and
values through the collections returned by the Keys and Values
properties. It is not necessary to regenerate the lists when the
properties are accessed, because the lists are just wrappers for the
internal arrays of keys and values.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 19 '06 #7
Dictionary<Units, Dictionary<Units, double>> is not really satisfactory
because, in general, you need to explicitly create Dictionary<Units,double>
and (worse), if you want to add to P2P_[Units.Pa] you must first check
whether or not it exists because it is a reference type.

There are three solutions - the first gives you the syntax that you want but
is quite tedious to code:

1) Create a struct StructDictionary<K,V> implementing IDictionary<K,V> that
lazily creates a Dictionary<K,V> and delegates everything to it. Then use
Dictionary<Units,StructDictionary<Units,double>>.

The second is shorter and more efficient but does have slightly different
capabilities and syntax.

2) Create a struct Pair<K1,K2> with Equals and GetHashCode overrides and use
Dictionary<Pair<Units,Units>,double>. This seems to give what you are
actually trying to do which is a unit conversion table.

In either case you should probably be hiding the whole thing in a class with
"double Convert(Units fromUnits,Units toUnits,double value)"

Note also that to be able to do all conversions you need a delegate rather
than a double since otherwise you cannot convert from centigrade to either
Fahrenheit or Kelvin. Alternatively conversions of the form y = mx+c can be
handled by a two part structure holding m and c. The data approach has the
advantage that you only need to populate the conversion one way - if the
conversion method can't find Fahrenheit to centigrade it can look up
centigrade to fahrenheit and invert it.

For maximum flexibility you should use strings rather than enums so that you
can load conversions from a database or file. (use static strings for
hardwired stuff)

"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:ON**************@TK2MSFTNGP03.phx.gbl...
"Torben Laursen" <To****@newsgroups.nospam> a écrit dans le message de
news:
e9**************@TK2MSFTNGP04.phx.gbl...

| I looked at that but I need a way to get a 2D map.
| This is my C++ code:
| enum Units {Pa=0, Bar, Atm, kPa, Psi, MPa};
| std::map<Units, std::map<Units, double> > P2P_;
|
| Then I can use P2P_ as: P2P_[Pa][Bar] where it returns a double
|
| It there a way to do this in C#?

How about Dictionary<Units, Dictionary<Units, double>> ??

public enum Units
{
Pa = 0, Bar, Atm, kPa, Psi, MPa
}

class Program
{
static void Main(string[] args)
{
Dictionary<Units, Dictionary<Units, double>> p2p = new
Dictionary<Units,Dictionary<Units,double>>();

Dictionary<Units, double> newEntry = new Dictionary<Units,double>();

newEntry.Add(Units.Atm, 12.34);

p2p.Add(Units.Pa, newEntry);

double result = p2p[Units.Pa][Units.Atm];

System.Console.WriteLine(result);

System.Console.ReadLine();
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Apr 20 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by Duane Hebert | last post: by
8 posts views Thread by Kin Pang | last post: by
44 posts views Thread by jmoy | last post: by
4 posts views Thread by Grey Plastic | last post: by
1 post views Thread by Saeed Amrollahi | last post: by
19 posts views Thread by Erik Wikström | last post: by
13 posts views Thread by kamaraj80 | last post: by
8 posts views Thread by mveygman | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.