473,385 Members | 1,927 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,385 software developers and data experts.

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 19856
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Duane Hebert | last post by:
2 questions: Given a map defined as std::map<int,string> stringmap; //How do you access the data_type (string) via an iterator? std::string spoo("doh");
8
by: Kin Pang | last post by:
Hi, I have a routine where I'm using a std::map from a pair of ints to double for caching evaluation results. The cache depth could be upto say 1000 in depth. However, my application needs to...
44
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice,...
4
by: Grey Plastic | last post by:
I do not understand why this code fails to compile (under gcc): #include <map> using namespace std; class Foo { map<Foo*,int> myMap; public: int lookup(const Foo& f) const { myMap.find(&f);...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
8
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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...

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.