C++ Map 容器

1.Map是c++的一個標準容器,它提供了很好一對一的關係。ios

Map是一種關聯是容器,在map中增長和刪除元素很是容易。能夠修改一個特定的節點而不對其餘節點不產生影響,因爲map是一種關聯式容器,Key--value結構咱們必須明確,在map中咱們能夠根據key的值修改其對應的value的值,而不能修改key的值,其最大的特色是key和value的值能夠是任意類型(內部類型你或用戶本身定義的類型)。c++

2. Map的特色:api

       (1)快速創建key--value的索引。key和value能夠是任意類型。app

       (2)map的查詢時間複雜度爲LogN,也就是若是包含1000個記錄,查詢次數最多爲10次,1,000,000個記錄,最多查找20次。函數

       (3)快速插入Key - Value 記錄。性能

       (4)快速刪除記錄spa

       (5)根據Key 修改value記錄。指針

       (6)遍歷全部記錄。對象

三、使用map排序

使用map得包含map類所在的頭文件

#include <map>

map對象是模板類,須要關鍵字和存儲對象兩個模板參數:std:map<int, string> personnel;

這樣就定義了一個用int做爲索引,並擁有相關聯的指向string的指針.

爲了使用方便,能夠對模板類進行一下類型定義,

typedef map<int, CString> UDT_MAP_INT_CSTRING;

UDT_MAP_INT_CSTRING enumMap;

四、在map中插入元素

改變map中的條目很是簡單,由於map類已經對[]操做符進行了重載

enumMap[1] = "One";

enumMap[2] = "Two";

.....

這樣很是直觀,但存在一個性能的問題。插入2時,先在enumMap中查找主鍵爲2的項,沒發現,而後將一個新的對象插入enumMap,鍵是2,值是一個空字符串,插入完成後,將字符串賦爲"Two"; 該方法會將每一個值都賦爲缺省值,而後再賦爲顯示的值,若是元素是類對象,則開銷比較大。咱們能夠用如下方法來避免開銷:

enumMap.insert(map<int, CString> :: value_type(2, "Two"))

五、查找並獲取map中的元素

下標操做符給出了得到一個值的最簡單方法:

CString tmp = enumMap[2];

可是,只有當map中有這個鍵的實例時纔對,不然會自動插入一個實例,值爲初始化值。

咱們可使用Find()和Count()方法來發現一個鍵是否存在。

查找map中是否包含某個關鍵字條目用find()方法,傳入的參數是要查找的key,在這裏須要提到的是begin()和end()兩個成員,分別表明map對象中第一個條目和最後一個條目,這兩個數據的類型是iterator.

int nFindKey = 2; //要查找的Key

//定義一個條目變量(實際是指針)

UDT_MAP_INT_CSTRING::iterator it= enumMap.find(nFindKey);

if(it == enumMap.end())

{

//沒找到

}

else {

//找到

}

經過map對象的方法獲取的iterator數據類型是一個std::pair對象,包括兩個數據 iterator->first 和 iterator->second 分別表明關鍵字和存儲的數據

六、從map中刪除元素

移除某個map中某個條目用erase()

該成員方法的定義以下

  1. iterator erase(iterator it); //經過一個條目對象刪除
  2. iterator erase(iterator first, iterator last); //刪除一個範圍
  3. size_type erase(const Key& key); //經過關鍵字刪除

clear()就至關於 enumMap.erase(enumMap.begin(), enumMap.end());

總結了一些map基本簡單實用的操做!
1. map最基本的構造函數;
   map<string , int >mapstring;         map<int ,string >mapint;
   map<sring, char>mapstring;         map< char ,string>mapchar;
   map<char ,int>mapchar;            map<int ,char >mapint;

2. map添加數據;

   map<int ,string> maplive;  
   1.maplive.insert(pair<int,string>(102,"aclive"));
   2.maplive.insert(map<int,string>::value_type(321,"hai"));
   3, maplive[112]="April";//map中最簡單最經常使用的插入添加!
3,map中元素的查找:

   find()函數返回一個迭代器指向鍵值爲key的元素,若是沒找到就返回指向map尾部的迭代器。        

   map<int ,string >::iterator l_it;; 
   l_it=maplive.find(112);
   if(l_it==maplive.end())
                cout<<"we do not find 112"<<endl;
   else cout<<"wo find 112"<<endl;
4,map中元素的刪除:
   若是刪除112;
   map<int ,string >::iterator l_it;;
   l_it=maplive.find(112);
   if(l_it==maplive.end())
        cout<<"we do not find 112"<<endl;
   else  maplive.erase(l_it);  //delete 112;
5,map中 swap的用法:
  Map中的swap不是一個容器中的元素交換,而是兩個容器交換;
  For example:
  #include <map>
  #include <iostream>

  using namespace std;

  int main( )
  {
      map <int, int> m1, m2, m3;
      map <int, int>::iterator m1_Iter;

      m1.insert ( pair <int, int>  ( 1, 10 ) );
      m1.insert ( pair <int, int>  ( 2, 20 ) );
      m1.insert ( pair <int, int>  ( 3, 30 ) );
      m2.insert ( pair <int, int>  ( 10, 100 ) );
      m2.insert ( pair <int, int>  ( 20, 200 ) );
      m3.insert ( pair <int, int>  ( 30, 300 ) );

   cout << "The original map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter->second;
      cout   << "." << endl;

   // This is the member function version of swap
   //m2 is said to be the argument map; m1 the target map
   m1.swap( m2 );

   cout << "After swapping with m2, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout  << "." << endl;
   cout << "After swapping with m2, map m2 is:";
   for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout  << "." << endl;
   // This is the specialized template version of swap
   swap( m1, m3 );

   cout << "After swapping with m3, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout   << "." << endl;
}

6.map的sort問題:
  Map中的元素是自動按key升序排序,因此不能對map用sort函數:
  For example:
  #include <map>
  #include <iostream>

  using namespace std;

 int main( )
 {
   map <int, int> m1;
   map <int, int>::iterator m1_Iter;

   m1.insert ( pair <int, int>  ( 1, 20 ) );
   m1.insert ( pair <int, int>  ( 4, 40 ) );
   m1.insert ( pair <int, int>  ( 3, 60 ) );
   m1.insert ( pair <int, int>  ( 2, 50 ) );
   m1.insert ( pair <int, int>  ( 6, 40 ) );
   m1.insert ( pair <int, int>  ( 7, 30 ) );

   cout << "The original map m1 is:"<<endl;
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout <<  m1_Iter->first<<" "<<m1_Iter->second<<endl;
  
}
  The original map m1 is:
  1 20
  2 50
  3 60
  4 40
  6 40
  7 30
  請按任意鍵繼續. . .

7,   map的基本操做函數:
      C++ Maps是一種關聯式容器,包含「關鍵字/值」對
      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的函數

聲明:僅摘錄,方便查找。

相關文章
相關標籤/搜索