Home All Groups Group Topic Archive Search About
Author
5 Dec 2008 3:40 PM
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

Author
5 Dec 2008 3:55 PM
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]
          - mvp@spam.guard.caspershouse.com

"Alex" <alsim***@hotmail.com> wrote in message
news:0eb150dd-cb44-4f9a-b79b-155eee6cbd37@k41g2000yqn.googlegroups.com...
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
Are all your drivers up to date? click for free checkup

Author
5 Dec 2008 4:20 PM
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


On Dec 5, 10:55 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
Show quoteHide quote
> 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]
>           - m...@spam.guard.caspershouse.com
>
> "Alex" <alsim***@hotmail.com> wrote in message
>
> news:0eb150dd-cb44-4f9a-b79b-155eee6cbd37@k41g2000yqn.googlegroups.com...
> 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
Author
5 Dec 2008 11:33 PM
Christophe Lephay
"Alex" <alsim***@hotmail.com> a écrit dans le message de groupe de
discussion :
7b9a037e-077c-4d50-bf3a-709a189b9***@j32g2000yqn.googlegroups.com...
> 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).

Show quoteHide quote
>
> On Dec 5, 10:55 am, "Nicholas Paldino [.NET/C# MVP]"
> <m...@spam.guard.caspershouse.com> wrote:
>> 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]
>> - m...@spam.guard.caspershouse.com
>>
>> "Alex" <alsim***@hotmail.com> wrote in message
>>
>> news:0eb150dd-cb44-4f9a-b79b-155eee6cbd37@k41g2000yqn.googlegroups.com...
>> 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
>

Bookmark and Share