#ifndef _STUDENTS_H_
#define _STUDENTS_H_
#include<map>
#include<string>
using namespace std;
typedef map<string, double> Data_type;
typedef map<string, Data_type> MyType;
namespace My_Code {
class Students
{
public:
Students();
~Students();
void AddStudent(const string& name, Data_type& data);
Data_type& GetScore(const string& name);
MyType& GetName();
void AddScore(const string& name, const string& course, const double& score);
void ShowScore(const string& name);
friend void ShowStudentsInfo(Students& s); //友函数
private:
MyType _name;
};
}
#endif
#include"Students.h"
#include<iostream>
#include<iomanip>
using namespace std;
using namespace My_Code;
Students::Students()
{
_name = MyType();
}
Students::~Students()
{
}
void Students::AddStudent(const string& name, Data_type& data)
{
_name[name] = data;
}
Data_type& Students::GetScore(const string& name)
{
return _name[name];
}
MyType& Students::GetName()
{
return _name;
}
void Students::AddScore(const string& name, const string& course, const double& score)
{
_name[name].insert(make_pair(course, score));
}
void Students::ShowScore(const string& name)
{
cout << name << ":\n";
Data_type::iterator it = _name[name].begin();
while (it != _name[name].end())
{
cout << setw(20) << it->first << ":" << it->second << endl;
it++;
}
}
void My_Code::ShowStudentsInfo(Students & s)
{
MyType::iterator it = s.GetName().begin(); //第一个人的指针
while (it != s.GetName().end()) //判断当前指针指向是否为最后一个学生
{
cout << it->first << ":\n"; //打印指针指向的学生
Data_type::iterator _it = it->second.begin(); //创建指向某个学生的第一门成绩
while (_it != it->second.end()) //判断指针指向的成绩是否为最后一门成绩
{
cout << setw(20) << _it->first << ":" << _it->second << endl;
_it++;
}
it++;
}
}
#include"Students.h"
#include<iostream>
#include<iomanip>
using namespace std;
using namespace My_Code;
int main(int argc, char** argv)
{
Students s;
Data_type m; //typedef map<string, Data_type> MyType; typedef map<string, double> Data_type;
m["Math"] = 60;
m["English"] = 70;
m["Chinese"] = 80;
s.AddStudent("ZhangSan", m);
s.AddStudent("LiSi", m);
s.AddStudent("WangEr", m);
/*
MyType::iterator it = s.GetName().begin();
while (it != s.GetName().end())
{
cout << it->first << ":\n";
Data_type::iterator _it = it->second.begin();
while (_it != it->second.end())
{
cout << setw(20) << _it->first << ":" << _it->second << endl;
_it++;
}
it++;
}
Data_type::iterator _it = s.GetScore("ZhangSan").begin();
cout << "ZhangSan:\n";
while (_it != s.GetScore("ZhangSan").end())
{
cout << setw(20) << _it->first << ":" << _it->second << endl;
_it++;
}
*/
cout << "===============================================" << endl;
s.ShowScore("ZhangSan");
cout << "===============================================" << endl;
s.AddScore("ZhangSan", "Physics", 78.5);
s.ShowScore("ZhangSan");
ShowStudentsInfo(s);
return 0;
}