C# 等效于 C++ map<string,double>

标签 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;

现在,我如何在 C# 中做同样的事情?

最佳答案

大致:-

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"]);

关于C# 等效于 C++ map<string,double>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1598070/

相关文章:

c# - 将两个不同的字符串列表合并到一个类列表中

c# - 显示空白的 XML 元素值

java - byte[] 到 String 和 String 到 byte[]

Java - map.get 不起作用,但元素在 map 中?

java - 如何从嵌套在 Java 中的 hashmap 和列表中的列表中访问和添加值

c# - WPF 用户控件,带有按钮和按键绑定(bind)

c# - 带管理等待的 Gui 重入

arrays - Ruby:通过将数组映射到数组来构建哈希

将三个数组复制到 C 中的 2 个暗数组

java - 具有空键功能的线程安全映射