agtbouns.cpp

来自「学习过C++程序设计语言(也就是说学习过Template)」· C++ 代码 · 共 40 行

CPP
40
字号
/////////////////////////////////////////////////
// 运行 cpl.bat 编译,生成 a.exe 文件,运行它。
// Write by MMT Studio
// 2005.01.14
/////////////////////////////////////////////////

#include <iostream>
#include <string>
#include <fstream>
#include <map>

using namespace std;
typedef map <string, double>::const_iterator CIT;

void main()
{
	map <string, double> bonuses;
	string agent;
	double bonus=0;

	ifstream bonusfile("bonuses.dat");
	if(!bonusfile)
	{
		// 报告出错信息并终止程序
		cout<<"open file error!"<<endl;
		exit(-1);
	}

	while (bonusfile >> agent >> bonus)
	{
		bonuses[agent]+=bonus;// 累加每个代理的奖金
	}

	// 显示 map 中的值,已经汇总
	for(CIT p=bonuses.begin(); p!=bonuses.end(); ++p)
	{
		cout << p->first <<'\t' << p->second <<endl;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?