C++面向对象程序设计 044:编程填空:数据库内的学生信息 ---- (北大Mooc)
发布日期:2021-06-30 22:28:02 浏览次数:2 分类:技术文章

本文共 1910 字,大约阅读时间需要 6 分钟。

文章目录


专题博客链接


前引

初学C++ 对于这道题来说确实

我对继承和 一些细节的地方把控不是很好
这个解答是借鉴的 德林恩宝的代码
写完这道题后 北大Mooc所有的题也差不多宣告结束了
最后的魔兽世界我也写不动了
之后也就转载一下吧

这个也就当作 初涉C++

之后的每一天也要开始尝试用C++刷Leetcode了
不再用C了 要习惯
然后再把C++ Prime给看完


原题题目

在这里插入图片描述

在这里插入图片描述

#include 
#include
#include
#include
#include
using namespace std;// 在此处补充你的代码struct Student {
string name; int score;};template
void Print(T first,T last) { for(;first!= last; ++ first) cout << * first << ","; cout << endl;}int main(){ Student s[] = { { "Tom",80},{ "Jack",70}, { "Jone",90},{ "Tom",70},{ "Alice",100} }; MyMultimap
mp; for(int i = 0; i<5; ++ i) mp.insert(make_pair(s[i].name,s[i].score)); Print(mp.begin(),mp.end()); //按姓名从大到小输出 mp.Set("Tom",78); //把所有名为"Tom"的学生的成绩都设置为78 Print(mp.begin(),mp.end()); MyMultimap
> mp2; for(int i = 0; i<5; ++ i) mp2.insert(make_pair(s[i].score,s[i].name)); Print(mp2.begin(),mp2.end()); //按成绩从小到大输出 mp2.Set(70,"Error"); //把所有成绩为70的学生,名字都改为"Error" Print(mp2.begin(),mp2.end()); cout << "******" << endl; mp.clear(); string name; string cmd; int score; while(cin >> cmd ) { if( cmd == "A") { cin >> name >> score; if(mp.find(name) != mp.end() ) { cout << "erroe" << endl; } mp.insert(make_pair(name,score)); } else if(cmd == "Q") { cin >> name; MyMultimap
::iterator p = mp.find(name); if( p!= mp.end()) { cout << p->second << endl; } else { cout << "Not Found" << endl; } } } return 0;}

代码实现

template
>class MyMultimap:public multimap
{
public: void Set(key str,T score) {
typename multimap
::iterator p1,p2; p1 = multimap
::lower_bound(str) ; p2 = multimap
::upper_bound(str); for(;p1!= p2;p1++) {
if(p1->first == str) p1->second = score; } }};template
ostream& operator <<(ostream& os,const pair
& p){ os<<'('<
<<','<
<<')'; return os;}

转载地址:https://love6.blog.csdn.net/article/details/115057110 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C++面向对象程序设计 045:魔兽世界三(开战) ---- (北大Mooc)
下一篇:C++面向对象程序设计 043:冷血格斗场 ---- (北大Mooc)

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年05月02日 05时30分45秒