用户登录
用户注册

分享至

C# 等价于 C++ 映射<string,double>

  • 作者: 切克闹21628686
  • 来源: 51数据库
  • 2023-02-13

问题描述

我想为不同的帐户保留一些总计.在 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"]);
软件
前端设计
程序设计
Java相关