~cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
vector<string> tokenize(const string& line);
bool save_map(vector<string>&, map< string, vector<int> >&);
double total(const vector<int>&);
bool print_report(ostream&,
const map< string, vector<int> >,
double accu(const vector<int>&) = total);
int main(int argc, char *argv[]) {
string line;
vector<string> token;
map< string, vector<int> > grades;
ifstream fin("input.txt");
while(getline(fin, line)) {
token = tokenize(line);
save_map(token, grades);
}
print_report(cout, grades);
system("PAUSE");
return EXIT_SUCCESS;
}
vector<string> tokenize(const string& line) {
string word;
vector<string> ret;
int beg=0;
int quantity=0;
for(int i=0; i != line.size(); i++) {
if (line[i] == ' ') {
ret.push_back(line.substr(beg, quantity));
beg=i+1;
quantity=0;
} else ++quantity;
}
return ret;
}
bool save_map(vector<string>& input, map< string, vector<int> >& grades) {
for (int i = 1; i != input.size(); i++) {
grades[input[0]].push_back(atoi(input[i].c_str()));
}
return true;
}
double total(const vector<int>& grades) {
return accumulate(grades.begin(), grades.end(), 0.0);
}
bool print_report(ostream& os,
const map< string, vector<int> > record,
double accu(const vector<int>&)) {
for(map< string, vector<int> >::const_iterator iter = record.begin();
iter != record.end();
iter++) {
os<<iter->first<<'\t';
for(vector<int>::const_iterator grades = (iter->second).begin();
grades != (iter->second).end();
++grades)
cout<<*grades<<'\t';
cout<<accu(iter->second)<<'\t';
cout<<accu(iter->second)/(iter->second.size())<<'\t';
cout<<endl;
}
return true;
}