C++ C++;:如何计算阵列中唯一客户端的数量并输出其支出?
如果我有两个数组,一个存储客户的名称,另一个存储他们花费的金额,如何分别输出他们的个人支出 比如说, Array1:[peter,mary,peter,may,edward] Array2:[300,400,500,300,400,500] 阵列1:[彼得、玛丽、彼得、五月、爱德华] 阵列2:[300400500300400500] 花费的钱的位置与名字相对应,并且每个人都花费了正美元 我知道阵列的大小,但如何输出它们的个人支出并计算客户端的数量 由于客户机在数组中可能有多条记录,我对如何分别计算数字和输出感到有点困惑 预期: **Spendings:** peter:600 mary:400 ... ... **Number of people**:4 **支出:** 彼得:600 玛丽:400 ... ... **人数**:4人C++ C++;:如何计算阵列中唯一客户端的数量并输出其支出?,c++,arrays,text,C++,Arrays,Text,如果我有两个数组,一个存储客户的名称,另一个存储他们花费的金额,如何分别输出他们的个人支出 比如说, Array1:[peter,mary,peter,may,edward] Array2:[300,400,500,300,400,500] 阵列1:[彼得、玛丽、彼得、五月、爱德华] 阵列2:[300400500300400500] 花费的钱的位置与名字相对应,并且每个人都花费了正美元 我知道阵列的大小,但如何输出它们的个人支出并计算客户端的数量 由于客户机在数组中可能有多条记录,我对如何分别计
这是我以前的想法(很抱歉,我忘了在我原来的问题中包括这一点):
int数组\金额\存储[5]//用于存储每个客户的金额
对于(int i=0;i请查看或。使用客户名称作为其键
,并将其货币金额作为其值
。然后,您可以简单地在数组中循环,将每个名称的购买量相加,然后在完成后,在映射中循环以输出结果。例如:
#include <iostream>
#include <map>
#include <iomanip>
std::map<std::string, double> ClientSpending;
for(int i = 0; i < NumberOfArrayElements; ++i)
ClientSpending[Array1[i]] += Array2[i];
std::cout << "Spendings:" << std::endl;
for (auto &client : ClientSpending)
std::cout << client.first << ":" << std::put_money(client.second) << std::endl;
std::cout << std::endl;
std::cout << "Number of people:" << ClientSpending.size() << std::endl;
#包括
#包括
#包括
标准::地图客户支出;
对于(int i=0;i 很抱歉,我忘了添加我的工作,我要编辑原始问题。非常感谢,我已经在这个独特的输出问题上坚持了几天了,谢谢你的帮助。
#include <iostream>
#include <map>
#include <iomanip>
std::map<std::string, double> ClientSpending;
for(int i = 0; i < NumberOfArrayElements; ++i)
ClientSpending[Array1[i]] += Array2[i];
std::cout << "Spendings:" << std::endl;
for (auto &client : ClientSpending)
std::cout << client.first << ":" << std::put_money(client.second) << std::endl;
std::cout << std::endl;
std::cout << "Number of people:" << ClientSpending.size() << std::endl;