vectors and maps in C#

A

Alex

Hello everybody,

I’m new to C# and have a very general question. In my C++ project I’m
widely using STL library to work with vectors and maps. As I
understand C# arrays allow me to have something like vectors, i.e. I
can create some class “MyClass” and declare array

MyClass[] ar_myclasses;

And here is my question – is in C# something similar to STL maps? I
mean can I have some class, where some “key” will point to some
element of some class. Let’s say

MapClass1:

[key1] -> [string1]
[key2] -> [string2]
[key3] -> [string3]
………………………

or in my case

MapClass2:

[key1] -> [myclass1, myclass2, myclass3…]
[key2] -> [myclass4, myclass5, myclass6…]
[key3] -> [myclass7, myclass8, myclass9…]
…………………….

Regards,
Alex
 
N

Nicholas Paldino [.NET/C# MVP]

Alex,

I believe that List<T> ("list of T", like templates, but not, these are
Generic, in case you didn't know) is what you are looking for to replace
vectors. It auto-allocates memory as needed when new elements are added, so
you don't have to worry about exceeding the bounds like you would with an
array.

To replace a map, you want to use Dictionary<TKey, TValue> which
provides the mapping that you want. In your example, it would seem you want
to have a Dictionary<TKey, List<TValue>>, where each key returns a vector of
instances.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello everybody,

I’m new to C# and have a very general question. In my C++ project I’m
widely using STL library to work with vectors and maps. As I
understand C# arrays allow me to have something like vectors, i.e. I
can create some class “MyClass” and declare array

MyClass[] ar_myclasses;

And here is my question – is in C# something similar to STL maps? I
mean can I have some class, where some “key” will point to some
element of some class. Let’s say

MapClass1:

[key1] -> [string1]
[key2] -> [string2]
[key3] -> [string3]
………………………

or in my case

MapClass2:

[key1] -> [myclass1, myclass2, myclass3…]
[key2] -> [myclass4, myclass5, myclass6…]
[key3] -> [myclass7, myclass8, myclass9…]
…………………….

Regards,
Alex
 
A

Alex

Thank you Nicholas very much,

That's exactly kind of answer I was expecting.
I'll start looking into (learning) List and Dictionary implementation
right away.

Best regards,
Alex
 
C

Christophe Lephay

"Alex" <[email protected]> a écrit dans le message de groupe de
discussion :
(e-mail address removed)...
Thank you Nicholas very much,

That's exactly kind of answer I was expecting.
I'll start looking into (learning) List and Dictionary implementation
right away.

You could have a look at SortedDictionary or SortedList as well, which are
closer to maps (because elements are ordered).

Unfortunatelyn there is no equivalent to multimap (as far as i know).
Alex,

I believe that List<T> ("list of T", like templates, but not, these are
Generic, in case you didn't know) is what you are looking for to replace
vectors. It auto-allocates memory as needed when new elements are added,
so
you don't have to worry about exceeding the bounds like you would with an
array.

To replace a map, you want to use Dictionary<TKey, TValue> which
provides the mapping that you want. In your example, it would seem you
want
to have a Dictionary<TKey, List<TValue>>, where each key returns a vector
of
instances.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hello everybody,

I’m new to C# and have a very general question. In my C++ project I’m
widely using STL library to work with vectors and maps. As I
understand C# arrays allow me to have something like vectors, i.e. I
can create some class “MyClass” and declare array

MyClass[] ar_myclasses;

And here is my question – is in C# something similar to STL maps? I
mean can I have some class, where some “key” will point to some
element of some class. Let’s say

MapClass1:

[key1] -> [string1]
[key2] -> [string2]
[key3] -> [string3]
………………………

or in my case

MapClass2:

[key1] -> [myclass1, myclass2, myclass3…]
[key2] -> [myclass4, myclass5, myclass6…]
[key3] -> [myclass7, myclass8, myclass9…]
…………………….

Regards,
Alex
 

Ask a Question

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.

Ask a Question

Similar Threads


Top