Thread: C# equivalents of STL containers?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    C# equivalents of STL containers?

    I've got a lot of experience in C++, just looking at C# now. The basics seem easy enough, but what I've not yet found are good analogs to STL containers in C++.

    I'm just wondering what the C# equivalents to certain C++ containers are:

    std::vector -- Probably System.Collections.Arraylist?
    std::list
    std::map -- Probably System.Collections.Hashtable?
    std::set -- I suppose one could use System.Collections.Hashtable and discard the value.
    std::multimap
    std::multiset
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Roll everything up into List<> and Dictionary<> and that's how it is in C#. You also have sorted versions of those containers too, but I assume the idea is not to worry about such "trivial" matters as the difference between a vector and a linked list.
    My best code is written with the delete key.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Generally it's System.Collections and System.Collections.Generic in C#2.

    But if you want my opinion, what .Net offers is a joke. Java has got a far more complete and interesting container collection.

    There is no linked list. There's AFAICR no tree map (which is what std::map effectively is). I can't remember anything about a set.


    Prelude, your new avatar is irritating.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Agreed Prelude. Switch back
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Agreed Prelude. Switch back
    Okay, but how far back? How about number 1?
    My best code is written with the delete key.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Nice
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by CornedBee
    There is no linked list.
    Erm, LinkedList<>?

    I generally use the following to replicate similar data structures from the STL.
    std::vector - List<T>
    std::list - LinkedList<T>
    std::map - Dictionary<Tkey, Tvalue>
    std::set - Dictionary<Tkey, Tvalue> (with null values)
    std::multimap - Dictionary<Tkey, List<Tvalue>>
    std::multiset - Dictionary<Tkey, int> (with int keeping count of the number of Tkeys)
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Erm, LinkedList<>?
    I took a look and I was mistaken. It seems that 2.0 has List<>, Stack<>, Queue<>, Dictionary<>, SortedList<>, SortedDictionary<>, and LinkedList<>. That's a much better selection that I thought before and maps more closely to the STL if you want to do a comparison.

    >I generally use the following to replicate similar data structures from the STL.
    I generally tailor my solutions to .NET rather than try to apply relations that might not be present. Then again, I also write my own data structures about half the time, so I might not be the best person to listen to.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Actually piano, having played with C# and reading a lot of documentation pages once I was given a kick in the right direction, it seems SortedDictionary<> is closest to std::set and std::map in that it sorts by key (often nice) and it seems to internally use a balanced tree structure (judging by the big-O of the insertion/access/etc. times). Dictionary<> uses a hashtable which is a useful tool also.

    Nice idea though on the multiset/multimap!
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Wow...I didn't know about the SortedDictionary class. Sweet.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob STL question about Containers.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2009, 12:02 PM
  2. Bit arrays with STL containers
    By Mario F. in forum C++ Programming
    Replies: 23
    Last Post: 07-03-2007, 09:50 AM
  3. stl containers allocation in heap or stack?
    By sawer in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2006, 03:08 PM
  4. Pointer Elements & STL Containers :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 08:13 PM
  5. STL: element-wise addition of the contents of two containers
    By geophyzzer in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2002, 06:17 PM