映射

#include<iostream>
#include<map>
#include<string>
#include<utility>
using namespace std;
int main()
{
map<string, int>courses;
courses.insert(make_pair("CSAPP",3));
courses.insert(make_pair("C++",2));
courses.insert(make_pair("CSARCH",4));
courses.insert(make_pair("COMPILER",4));
courses.insert(make_pair("OS",5));
int n=3;
int sum=0;
while(n>0){
string name;
cin>>name;
auto iter=courses.find(name);
if(iter==courses.end()){
cout<<name<<" is not available"<<endl;
}
else{
sum+=iter->second;
courses.erase(iter);
n--;
}
}
cout<<"Total credit: "<<sum<<endl;
return 0;
}

posted @ 2023-05-05 20:24  不会JAVA的小袁  阅读(13)  评论(0编辑  收藏  举报