|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
vectors and maps in C#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 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 I’m new to C# and have a very general question. In my C++ project I’mnews:0eb150dd-cb44-4f9a-b79b-155eee6cbd37@k41g2000yqn.googlegroups.com... Hello everybody, 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 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 "Alex" <alsim***@hotmail.com> a écrit dans le message de groupe de 7b9a037e-077c-4d50-bf3a-709a189b9***@j32g2000yqn.googlegroups.com...discussion : > Thank you Nicholas very much, You could have a look at SortedDictionary or SortedList as well, which are > > That's exactly kind of answer I was expecting. > I'll start looking into (learning) List and Dictionary implementation > right away. 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 >
Other interesting topics
Accessing file extended properties in .NET
What good is .SELECT in LINQ anyway? seems useless compared to .where printDocument class...Form Feed Package access confusion Action of a button to a text box? Dealing with different types of objects in an ArrayList .NET version for extension methods? What's a proper way to organize the packages? How to load an assembly? How to completely disable Javascript in a Webbrowser control |
|||||||||||||||||||||||