1.练习项目:
练习使用map函数及其常用函数
2.选择课程
在蓝桥云课中选择课程《16届蓝桥杯省赛无忧班(C&C++ 组)4期》,选择第STL”课程16并开始练习。
3.开始练习
(1)源码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
map<int,string>mymap={{1,"apple"},{2,"banana"},{3,"orange"}};
mymap.insert({4,"grapes"});
cout<<"value at key 2: "<<mymap[2]<<endl;
for(const auto&pair:mymap){
cout<<"key: "<<pair.first<<",value: "<<pair.second<<endl;
}
mymap.erase(3);
if(mymap.count(3)==0){
cout<<"key 3 no found"<<endl;
}
mymap.clear();
if(mymap.empty()){
cout<<"map is empty"<<endl;
}
return 0;
}
(2)检验结果
对此代码进行检验,检验后无报错,提交此代码,判题结果为正确100分。
(3)练习心得:注意每段代码末尾的分号是否存在,如不存在则需即使补充;输入法是否切换为英语模式;语法是否错误。