C++标准容器库STL:map

typedef pair<string,Student>pair_t;
int main()
{
    map<string,Student> mapstu;
    mapstu.insert(pair_t("2",Student("delaiwen",23)));
    mapstu.insert(pair_t("1",Student("liqin",24)));
    mapstu.insert(pair_t("5",Student("sabo",21)));
//      插入
//    pair<map<string,Student>::iterator,bool> pr = mapstu.insert(pair_t("3",Student("lanbo",36)));
//    if(pr.second)     //pr.first->first  key
//    {                 //pr.first->second  value
//        cout<<"insert ok"<<endl;
//        pr.first->second.showStu();
//    }
//    else
//        cout<<"inset error"<<endl;
    map<string,Student>::iterator it=mapstu.begin();
//    for(;it!=mapstu.end();it++)
//        it->second.showStu();

//    mapstu["6"] = Student("hanbin",20);//插入,如果key不存在则会插入
//    mapstu["5"] = Student("hanbin",20);//如果key存在就会改值

//      获取(查看值)
//        mapstu.at("1").showStu();
//        mapstu.at("1") = Student("longxia",24);//1.
//        mapstu["1"].showStu();
//        mapstu["1"] = Student("longxia",24);  //2.
//    map<string,Student>::iterator it=mapstu.begin();  //3.
//    cout<<"key"<<" "<<it->first<<" "<<"value"<<" ";
//            it->second.showStu();
//    it->second = Student("longxia",22);
//        it = mapstu.find("5");    //4.通过find查找key 返回值是迭代子
//      if(it!=mapstu.end())
//      {   cout<<"find"<<endl;
//        it->second.showStu();
//      }
//      else
//          cout<<"not find"<<endl;
      //5.如果要通过value查找需要自己写算法
       //删除
//        mapstu.erase("1");
//        mapstu.erase(++mapstu.begin(),--mapstu.end());
//        mapstu.erase(it);

    //边界值
//    it=mapstu.upper_bound("2");
//    if(it!=mapstu.end())
//    {
//        cout<<"find "<<"key = "<<it->first<<" value=";
//              it->second.showStu();
//    }
//    it=mapstu.lower_bound("2");
//    if(it!=mapstu.end())
//    {
//        cout<<"find "<<"key = "<<it->first<<" value=";
//              it->second.showStu();
//    }
    //      双边界查找
//         pair<map<string,Student>::iterator ,map<string,Student>::iterator> pr= mapstu.equal_range("2");
//         if(pr.first!=mapstu.end())
//         {
//             cout<<"find lower_bound"<<" key ="<<pr.first->first<<" value= ";
//             pr.first->second.showStu();
//         }
//         else
//             cout<<"not find"<<endl;
//         if(pr.second!=mapstu.end())
//         {
//             cout<<"find upper_bound"<<" key ="<<pr.second->first<<" value= ";
//             pr.second->second.showStu();
//         }
//         else
//             cout<<"not find"<<endl;
//        for_each(mapstu.begin(),mapstu.end(),showmap);
}
#if 0 
map的容器特性
1.一次存两个数据key和value,以pair的形式存在,key和value一一对应,通过key来操作value
2.有序容器,对key进行排序
3.不允许key重复,允许value重复
4.增效率低,查询效率高

#endif

猜你喜欢

转载自www.cnblogs.com/xiaozoui11cl/p/12793153.html