C++ STL map

map是STL的一個關聯容器,對象是模板類,需要關鍵字key和存儲對象value兩個模板參數,內部形成相對應key - value,也可以理解爲輸入時的一對一的映射。

例如:std:map<int,string> int_str,這樣呢,我們就形成了一個以 int 作爲索引,指向string的指針。這裏的int,string 類型不唯一,你也可以使用int,int或者int , double或者string,double等等

  • begin() 返回指向map頭部的迭代器

  • clear() 刪除所有元素

  • count() 返回指定元素出現的次數

  • empty() 如果map爲空則返回true

  • end() 返回指向map末尾的迭代器

  • equal_range() 返回特殊條目的迭代器對

  • erase() 刪除一個元素

  • find() 查找一個元素

  • get_allocator() 返回map的配置器

  • insert() 插入元素

  • key_comp() 返回比較元素key的函數

  • lower_bound() 返回鍵值>=給定元素的第一個位置

  • max_size() 返回可以容納的最大元素個

  • rbegin() 返回一個指向map尾部的逆向迭代器

  • rend() 返回一個指向map頭部的逆向迭代器

  • size() 返回map中元素的個數

  • swap() 交換兩個map

  • upper_bound() 返回鍵值>給定元素的第一個位置

  • value_comp() 返回比較元素value的函數

key:

map的第一個關鍵字,也就是key,每個key只能在map中出現一次。

value:

map的第二個參數,也稱爲對應key的值。

map內部數據排列其實是內部自建一顆紅黑樹(非嚴格意義上的平衡二叉樹),這顆樹具有對數據自動排序的功能,所以在map內部所有的數據都是有序的,紅黑樹的特點是增加和刪除節點對迭代器的影響很微弱,操作的話只對操作節點有影響。

對於迭代器來說,可以修改實值,而不能修改key。

數據的插入與檢測

  1. insert函數插入pair數據
  2. insert函數插入value_type數據
  3. 以數組形式插入數據
#include <map>  
#include <string>  
#include <iostream>  
using namespace std;  
int main()  
{  
    map<int, string> int_str;               //個人覺得這樣命名簡單 
    int_str.insert( pair<int, string>(1, "I") );  
    int_str.insert( pair<int, string>(2, "LOVE") );  
    int_str.insert( pair<int, string>(3, "ME") );  
    
    int_str.insert(map<int, string>::value_type (4, "ACM"));  
    int_str.insert(map<int, string>::value_type (5, "CCPC"));  
    int_str.insert(map<int, string>::value_type (6, "ICPC")); 
    
	int_str[7] = "YUE";  
    int_str[8] = "TING";  
    int_str[9] = "ZHI"; 
    
    pair<map<int, string>::iterator, bool> boll_it; 
	 
    boll_it = int_str.insert( pair<int, string>(1, "YOU") );  //數據插入 與檢測 
    
    if(boll_it.second == true)  
        cout<<"YES"<<endl;  
    else  
        cout<<"NO"<<endl;  
    
    boll_it = int_str.insert(pair<int, string>(2, "NICE"));   //數據插入 與檢測 
    if(boll_it.second == true)  
        cout<<"FIND"<<endl;  
    else  
        cout<<"NO FIND"<<endl;     

    map <int, string>::iterator it;         //迭代器指針 
  
    for(it = int_str.begin(); it != int_str.end(); it++)  
       cout<<it->first<<' '<<it->second<<endl;
       
    cout<<endl<<endl<<endl;
       

       
    int_str[1] = "you"; 
    int_str[2] = "nice";
    
    for(it = int_str.begin(); it != int_str.end(); it++)  
       cout<<it->first<<' '<<it->second<<endl;  
}  

output:

NO
NO FIND
1 I
2 LOVE
3 ME
4 ACM
5 CCPC
6 ICPC
7 YUE
8 TING
9 ZHI



1 you
2 nice
3 ME
4 ACM
5 CCPC
6 ICPC
7 YUE
8 TING
9 ZHI


insert函數插入數據時,如果map中有這個關鍵字,insert插入失敗,與集合的唯一性有關。而數組方式則會插入,也可以說是覆蓋掉該關鍵字對應的值

map數據的多少計算

size函數

    int int_str_n = int_str.size();
    	cout<<int_str_n<<endl;

ps:這一小段程序放到上面程序段內就可以啦,小懶一下,嘻嘻

數據的遍歷

前向迭代器,反相迭代器,數組形式輸出

    for(it = int_str.begin(); it != int_str.end(); it++)          //正向輸出 
       cout<<it->first<<' '<<it->second<<endl;  
    	
    map<int, string>::reverse_iterator iter;  
    
    for(iter = int_str.rbegin(); iter != int_str.rend(); iter++)  //反向輸出 
        cout<<iter->first<<"  "<<iter->second<<endl;  
        
    for(int i = 1; i <= int_str_n; i++)                           //數組形式輸出 
        cout<<int_str[i]<<endl; 

簡單加深掌握

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章