Ada*_*rce 61 c# arrays hashmap
我想为不同的帐户保留一些总计.在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;
Run Code Online (Sandbox Code Playgroud)
现在,我将如何在C#中做同样的事情?
ljs*_*ljs 85
大致:-
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 ${0}", accounts["Fred"]);
Run Code Online (Sandbox Code Playgroud)
use*_*783 14
尽管System.Collections.Generic.Dictionary匹配标记"hashmap"并且在您的示例中运行良好,但它并不完全等同于C++的std :: map - std :: map是一个有序集合.
如果排序很重要,您应该使用SortedDictionary.
| 归档时间: |
|
| 查看次数: |
138788 次 |
| 最近记录: |