I need a class that will work like C++ std::map. More specifically, I need such a behavior:
map< string, vector<int> > my_map;
Is it possible?
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I need a class that will work like C++ std::map. More specifically, I need such a behavior:
map< string, vector<int> > my_map;
Is it possible?
A dictionary is I believe what you want:
Dictionary<String, int> dict = new Dictionary<String, int>();
dict.Add("key", 0);
Console.WriteLine(dict["key"]);
etc, etc
MSDN: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
You can specify more or less any type as the key/value type. Including another dictionary, an array, or whatever:
Dictionary<String, String[]> dict = new Dictionary<String, String[]>();
So here each element in the Dictionary points to an array of strings.
To implement what you require (with the vector int), you would require a List as the value type:
Dictionary<String, List<int>> dict = new Dictionary<String, List<int>>();
It is worth noting that a Dictionary has no predefined order, whereas std::map does. If order is important, you may want to use SortedDictionary instead, which is almost identical in usage, but sorts on the key. All depends if you plan to iterate over the dictionary really.
Note however that if you use a class you created as the key, you will need to properly override GetHashCode and Equals.
It depends what you really need.
As it has been already said you get the lookup behaviour using System.Collections.Generic.Dictionary<Key, Value>
, so the equivalent to std::map<string, std::vector<int> >
would be (using System.Collections.Generic.List<int>
as vectorequivalent):
Dictionary<string, List<int>> myDictionary = new Dictionary<string, List<int>>();
myDictionary.Add("a", new List<int>());
and so on
Internally Dictionary uses a Hashtable, while std::map uses a Red-Black-Tree, so std::map is ordered, while Dictionary is unordered. If you need an ordered Dictionary (which would be more closely to std::map, you can use System.Collections.Generic.SortedDictionary<Key, Value>
. The usage is mostly identical which that of Dictionary
Yes, the declaration you have written in the question is correct. It maps a string onto a vector of ints. However, std::map is backed by a Red-Black tree implementation, and your question suggests you want a hash-table. If you can use boost, you could try their implementation of unordered_map. This is part of the tr1 specification, and implements the map as a hash-table. The hash functions for standard types are already implemented in boost, so you wouldn t need to worry about this.
#include <boost/unordered_map.hpp>
...
boost::unordered_map<std::string, std::vector<int> > my_map;
If your goal is to replace map, then you want SortedDictionary , because that also implements a red-black tree. If you want a Hash Table, then Dictionary will work.
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...