关闭

C++程序设计语言练习6.3 multimap标准库的使用

标签: C++标准库
122人阅读 评论(0) 收藏 举报
分类:

代码如下:

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

using std::cin;
using std::cout;
using std::multimap;
using std::string;
using std::pair;

int main()
{
	string str;
	float f;

	multimap<string,float> name;
	while(cin>>str>>f){
		name.insert(pair<string,float>(str,f));
	}

	multimap<string,float>::const_iterator citer = name.begin();

	while(citer != name.end()){
		float sum = 0;
		string key = citer->first;
		int count = name.count(citer->first);
		for(int i = 0;i<count; i++){
			sum += citer->second;
			citer++;
		}
		cout<<"the sum of key "<< key<<" is:"<<sum<<
		" and the average is "<< sum/count<<'\n';
	}

	citer = name.begin();
	float sum = 0;
	float average = 0;
	for (; citer != name.end(); ++citer)
	{
		sum += citer->second;
	}
	cout << "the sum of map is:"<<sum<<
	" and the average is "<<sum/name.size()<<'\n';
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16831次
    • 积分:597
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:10篇
    • 译文:0篇
    • 评论:2条
    最新评论