主要介绍了c++STL-map的插入、查找、删除、清空等操作
```cpp
#include<iostream>
#include<map>
using namespace std;
map<int,string>mp;
void mapInsert(){
auto res0=mp.insert(make_pair(000,"student_zero"));
if(res0.second){
cout<<"插入成功"<<endl;
}else{
cout<<"插入失败"<<endl;
}
auto res1=mp.insert(pair<int,string>(001,"student_one"));
if(res1.second){
cout<<"插入成功"<<endl;
}else{
cout<<"插入失败"<<endl;
}
auto res2=mp.insert(map<int,string>::value_type(002,"student_two"));
if(res2.second){
cout<<"插入成功"<<endl;
}else{
cout<<"插入失败"<<endl;
}
auto res3=mp.emplace(003,"student_thrid");
if(res3.second){
cout<<"插入成功"<<endl;
}else{
cout<<"插入失败"<<endl;
}
mp[004]="student_two";
}
void mapFind(){
auto it = mp.find(001);
if (it != mp.end()) {
cout<<"成功找到了"<< it->second << endl;
} else {
cout << "没有找到" << endl;
}
int n = mp.count(001);
if (n > 0) {
cout << "找到了" << endl;
} else {
cout << "没有找到" << endl;
}
}
bool is_odd(const pair<int, string>& p) {
return p.first % 2 != 0;
}
void mapDelete(){
int n;
n = mp.erase(001);
if (n > 0) {
cout << "删除成功" << endl;
} else {
cout << "删除失败" << endl;
}
auto it1 = mp.find(002);
auto it2 = mp.find(003);
if (it1 != mp.end() && it2 != mp.end()) {
mp.erase(it1, it2);
cout << "删除成功" << endl;
} else {
cout << "删除失败" << endl;
}
auto it = mp.begin();
while (it != mp.end()) {
if (is_odd(*it)) {
it = mp.erase(it);
} else {
++it;
}
}
for (auto it = mp.begin(); it != mp.end(); ++it) {
cout << it->first << " " << it->second << endl;
}
}
void mapClear(){
mp.erase(mp.begin(), mp.end());
mp.clear();
if (mp.empty()) {
cout << "清空成功" << endl;
} else {
cout << "清空失败" << endl;
}
map<int,string>().swap(mp);
if (mp.empty()) {
cout << "清空成功" << endl;
} else {
cout << "清空失败" << endl;
}
}
int main(){
mapInsert();
mapFind();
mapDelete();
mapClear();
cout << "map size: " << mp.size() << endl;
}`在这里插入代码片`