本文介绍了C#相当于C ++地图&LT的;字符串,双>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想保留一些总计不同的帐户。在C ++中我会使用STL是这样的:
I want to keep some totals for different accounts. In C++ I'd use STL like this:
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#?
Now, how would I do the same thing in 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 ++地图&LT的;字符串,双&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!