C :如何计算数组中唯一客户端的数量并输出其支出
C++: How to count the numbers of unique clients in array and output their spending?
如果我有两个数组,一个存储客户名称,另一个存储他们所花费的数量,我该如何分别输出他们的个人支出?
例如,
array1:[彼得,玛丽,彼得,梅,爱德华]array2:[300,400,500,300,400,500]
所花费的钱的位置与名称相对应,每个人都花了阳性的钱。
我知道数组的大小,但是如何输出他们的个人支出并计算客户次数?
由于客户端可能在数组中具有多个记录,因此我对如何分别计数数字和输出感到有些困惑。
预期:
**支出:**彼得:600玛丽:400......**人数**:4
这是我以前的想法(很抱歉忘记将其包含在我的原始问题中(:
int Array_amount_store[5]; //For storing each clients' amount
for (int i=0; i<=5; i=i+1) // Initializing
Array_amount_store[i]=0;
for (int i=0; i<=5; i=i+1)
for (int j=0; j<=5; j=j+1)
if (Array1[j]==Array1[i])
Array_amount_store[i]=Array_amount_store[i]+Array2[i];
我刚刚计算了总数,但陷入了如何输出。
请看一下std::map
或std::unordered_map
。将客户名称作为其key
,其货币总和作为其value
。然后,您可以简单地循环浏览每个名称的购买阵列,然后完成后,循环循环以输出结果。例如:
#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;
相关文章:
- "unknown ca"自生成的 CA、证书和客户端/服务器
- 如何将函数集合传递给客户端类,以便将它们当作客户端类本身的成员使用
- 使用调试/崩溃报告将应用程序部署到客户端
- 如何在本地机器上运行c++和javascript客户端代码(hackerbank风格)
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 从服务器传输到客户端的消息不会出现
- OpenSSL TLS服务器-使用客户端证书白名单
- 当服务中的事件被触发时,如何将响应从服务发送回客户端?
- 我可以与 python 服务器而不是 c++ 客户端建立 tcp/ip 套接字吗?
- 提升 Asio TCP 服务器 处理多个客户端
- boost::asio UDP 广播客户端仅接收"fast"数据包
- 如何绑定 C++ gRPC 客户端的网络接口
- C++套接字客户端到 Python 服务器未创建连接
- 用于解析 win64 堆栈跟踪的命令行客户端(可以访问符号服务器)
- 将相机数据从服务器实时流式传输到客户端
- 如何将 Firebase 与基于 Linux 的客户端应用配合使用,以便与服务器进行双向消息通信
- Boost-Beast 异步 Web 套接字 服务器-客户端异步读写 不在控制台上写入输出
- C :如何计算数组中唯一客户端的数量并输出其支出
- 如何将OpenCV的输出显示或流式传输到HTML页面或其他一些客户端应用程序
- c++套接字客户端在输出流关闭的情况下不断从Java服务器事件中接收一些内容