HashTable是否存在任何通用版本?

Ser*_*gey 0 c# c++ hashtable stdmap

我需要一个像C++ std :: map一样工作的类.更具体地说,我需要这样的行为:
map< string, vector<int> > my_map;
这可能吗?

Kaz*_*zar 12

字典是我相信你想要的:

Dictionary<String, int> dict = new Dictionary<String, int>();

dict.Add("key", 0);
Console.WriteLine(dict["key"]);
Run Code Online (Sandbox Code Playgroud)

等等

MSDN:http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

您可以指定更多或更少的任何类型作为键/值类型.包括另一个字典,数组或其他:

Dictionary<String, String[]> dict = new Dictionary<String, String[]>();
Run Code Online (Sandbox Code Playgroud)

所以这里Dictionary中的每个元素都指向一个字符串数组.

要实现所需(使用vector int),您需要List作为值类型:

Dictionary<String, List<int>> dict = new Dictionary<String, List<int>>();
Run Code Online (Sandbox Code Playgroud)

值得注意的是,Dictionary没有预定义的顺序,而std :: map则没有.如果订单很重要,您可能希望使用SortedDictionary,这在使用上几乎相同,但对密钥进行排序.一切都取决于你是否打算真正迭代字典.

但请注意,如果使用您创建的类作为键,则需要正确覆盖GetHashCode和Equals.