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

代码如下:

#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';
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/armor51/article/details/51556612
文章标签: C++ 标准库
个人分类: C++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭