代码如下:
#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';
}