STL學習筆記-map/multimap容器

簡介:
map是標準的關聯式容器,一個map是一個鍵值對的序列,即(key,value)。提供基於key的快速檢索能力
map中key的值是唯一的。map中的元素按照一定的順序排列,元素插入是按照排序規則插入的,不能指定位置插入
map的具體實現是紅黑樹變體的平衡二叉樹數據結構。插入和刪除比vector快
map可直接取key對應的value,如map[key] = value
multimap和map區別在於:map中同一個key只能出現一次,而multimap中相同鍵可以出現多次,但是multimap不支持[]操作

頭文件:
#include<map>

map/multimap的默認構造:
map<T1, T2> maptt;
multimap<T1, T2> multimaptt;

map的插入和迭代器:
map<int, string> map1;
//四種插入方式
map1.insert(pair<int, string>(1, "sudent1"));
map1.insert(make_pair(2, "student2"));
map1.insert(map<int, string>::value_type(3, "student3"));
map1[4] = "stident4";
//遍歷
for(map<int, string>::iterator it = map1.begin(); it != map1.end(); it++){
     cout<<it->first<<":" << it->second<<endl;    
}
//刪除元素
while(!map1.empty()){
         map<int, string>::iterator it = map1.begin();

         map1.erase(it);
}
//四種插入方法的區別:
//前三種直接用insert的方法返回的都是pair<iterator, bool>, 第一個是插入元素對應的迭代器的位置
pair<map<int, string>::iterator, bool> pair1 = map1.insert(make_pair(2, "student22"));
if(pair1.second == true)  
         cout << "insert success." << endl;
else
         cout << "insert falied." << endl; //此時會打印出錯誤信息,插入失敗
//第四種插入方法會覆蓋已經存在的鍵值對
map1[4] = "stident44"; 

map的查找和異常處理:
map.fine(key); // 若找到,返回迭代器,否則返回map.end()
map.count(key); //返回map中改鍵爲key的對組的個數,對map要麼是0或者1,multimap可能大於1
//map也支持lower_bound upper_bound equal_range等操作,和set一樣
pair(map<int, string>::iterator, map<int, string>::iterator) mypair = map1.equal_range(5);
//第一個是>=5的位置 第二個是>5的位置
if(mypair.first != map1.end()){
     cout << mapair.first->first << ":" << mapair.first->second<<endl;
}
if(mypair.second!= map1.end()){
     cout << mapair.second->first << ":" << mapair.second->second<<endl;
}

multimap案例:
//multimap最大的特點就是一個key對應多個value ----> 可以用來對數據進行分組
// 一個公司有多個部門: sale 2人  development 1人  financial 2人
//人員信息有: 姓名 電話 工資
//通過multimap進行信息的插入 保存 顯示 可以分部門顯示員工的信息
class Person{
     public:
          string    name;
          int          age;
          string     tel;
          double  salary;
           Person(string name, int age){
             this->name = name;
              this->age = age; 
            }
}
 void main(){
     Person p1("zhang1", 32);    
     Person p2("zhang2", 33);    
     Person p3("wang1", 34);    
     Person p4("wang2", 35);    
     Person p5("wang3", 36);
     map<string, Person> map1;
     map1.inster(make_pair("sale", p1));   
     map1.inster(make_pair("sale", p2));   
     map1.inster(make_pair("development", p3));   
     map1.inster(make_pair("financial", p4));   
     map1.inster(make_pair("financial", p5));   

     //遍歷
     for( map<string, Person>::iterator it = map1.begin(); it != map1.end; it++){
             cout<< it->first<<":"<<it->second.name<<endl;
     }
     //計數
     int num = map1.count("financial");
     cout<<"financial person counts:"<<num<<endl;
     
     //找指定鍵的所有值
     map<string, Person>::iterator it2 = map1.find("financial");
     int tag = 0;
     while(it2 != map1.end() && tag < num){
        cout << it2->first<<":"<< it2->second.name<<endl;
        it2++;
        tag++;
     }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章