C# 等效于 C++ map<string,double>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1598070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# equivalent of C++ map<string,double>
提问by Adam Pierce
I want to keep some totals for different accounts. In C++ I'd use STL like this:
我想为不同的帐户保留一些总数。在 C++ 中,我会像这样使用 STL:
map<string,double> accounts;
// Add some amounts to some accounts.
accounts["Fred"] += 4.56;
accounts["George"] += 1.00;
accounts["Fred"] += 1.00;
cout << "Fred owes me $" << accounts['Fred'] << endl;
Now, how would I do the same thing in C# ?
现在,我将如何在 C# 中做同样的事情?
采纳答案by ljs
Roughly:-
大致:-
var accounts = new Dictionary<string, double>();
// Initialise to zero...
accounts["Fred"] = 0;
accounts["George"] = 0;
accounts["Fred"] = 0;
// Add cash.
accounts["Fred"] += 4.56;
accounts["George"] += 1.00;
accounts["Fred"] += 1.00;
Console.WriteLine("Fred owes me Dictionary<string, double> accounts;
", accounts["Fred"]);
回答by Daniel A. White
static void Main(string[] args) {
String xml = @"
<transactions>
<transaction name=""Fred"" amount=""5,20"" />
<transaction name=""John"" amount=""10,00"" />
<transaction name=""Fred"" amount=""3,00"" />
</transactions>";
XDocument xmlDocument = XDocument.Parse(xml);
var query = from x in xmlDocument.Descendants("transaction")
group x by x.Attribute("name").Value into g
select new { Name = g.Key, Amount = g.Sum(t => Decimal.Parse(t.Attribute("amount").Value)) };
foreach (var item in query) {
Console.WriteLine("Name: {0}; Amount: {1:C};", item.Name, item.Amount);
}
}
回答by Daniel Pryden
You want the Dictionaryclass.
你想要Dictionary类。
回答by Jim Schubert
Dictionary is the most common, but you can use other types of collections, e.g. System.Collections.Generic.SynchronizedKeyedCollection, System.Collections.Hashtable, or any KeyValuePair collection
字典是最常见的,但您可以使用其他类型的集合,例如 System.Collections.Generic.SynchronizedKeyedCollection、System.Collections.Hashtable 或任何 KeyValuePair 集合
回答by Ricardo Lacerda Castelo Branco
This code is all you need:
这段代码就是你所需要的:
##代码##And the content is:
内容是:
Name: Fred; Amount: R$ 8,20;
Name: John; Amount: R$ 10,00;
姓名:弗雷德;金额:R$ 8,20;
姓名:约翰;金额:10,00 雷亚尔;
That is the way of doing this in C# - in a declarative way!
这就是在 C# 中执行此操作的方式 - 以声明方式!
I hope this helps,
我希望这有帮助,
Ricardo Lacerda Castelo Branco
里卡多·拉塞尔达 Castelo Branco
回答by user200783
Although System.Collections.Generic.Dictionary matches the tag "hashmap" and will work well in your example, it is not an exact equivalent of C++'s std::map - std::map is an ordered collection.
尽管 System.Collections.Generic.Dictionary 匹配标签“hashmap”并且在您的示例中运行良好,但它并不完全等同于 C++ 的 std::map - std::map 是有序集合。
If ordering is important you should use SortedDictionary.
如果排序很重要,您应该使用SortedDictionary。
回答by Paul
While we are talking about STL, maps and dictionary, I'd recommend taking a look at the C5library. It offers several types of dictionaries and maps that I've frequently found useful (along with many other interesting and useful data structures).
当我们谈论 STL、地图和字典时,我建议您查看一下C5库。它提供了我经常发现有用的几种类型的字典和地图(以及许多其他有趣和有用的数据结构)。
If you are a C++ programmer moving to C# as I did, you'll find this library a great resource (and a data structure for this dictionary).
如果你是一名 C++ 程序员,像我一样转向 C#,你会发现这个库是一个很好的资源(以及这个字典的数据结构)。
-Paul
-保罗
回答by Dejavu
The closest equivalent of C++ std::map<>
(a tree internally) is C# OrderedDictionary<>
(a tree internally), while C# OrderedDictionary<>
is missing some very important methods from C++ std::map<>
, namely: std::map::find
, std::map::lower_bound
, std::map::upper_bound
, std::map::equal_range
, and std::map
iterators
, which are basically the backbone for the previous 4 methods.
最近当量C ++ std::map<>
(树内部)是C# OrderedDictionary<>
(树内部),而C#OrderedDictionary<>
是缺少由C一些非常重要的方法++ std::map<>
,即:std::map::find
,std::map::lower_bound
,std::map::upper_bound
,std::map::equal_range
,和std::map
iterators
,这是基本上是以前的4种方法的骨干。
Why those 4 methods are important? Because it gives us the ability to locate the "whereabouts" of a given key, in addition to only being able to check if a key exists, or the SortedDictionary is guaranteed to be ordered.
为什么这 4 种方法很重要?因为它使我们能够定位给定键的“下落”,此外只能检查键是否存在,或者保证 SortedDictionary 是有序的。
What is "whereabouts" of a key in a std::map
? The key doesn't necessarily have to exist in the collection, we want to know the location the key might be at, usually between two iterators pointing to two adjacent existing keys respectively in the collection, so we can operate on the rangethe key falls into in a O(logN)
complexity. Without such 4 methods (with iterators), one has to do an O(N)
iteration through the collection every time a range is queried against a key.
a 中钥匙的“下落”是std::map
什么?键不一定必须存在于集合中,我们想知道键可能所在的位置,通常在两个迭代器之间,分别指向集合中的两个相邻的现有键,因此我们可以对键所在的范围进行操作进入O(logN)
复杂性。如果没有这 4 种方法(使用迭代器),则O(N)
每次针对键查询范围时都必须对集合进行迭代。