C++:STL常用模塊總結(map)

map

map又稱爲哈希表,是一個由標記值(key value)和映射(mapped value)組成的關係列表,其中標記值將映射值進行排序和整理,每一個標記值對應着一個映射值,map在通過標記值找到映射值的過程比unordered_map慢,但是可以通過指針依照排放順序來進行操作。

使用之前引用

#include <map>

定義方法重載函數彙總

empty (1):  
explicit map (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
explicit map (const allocator_type& alloc);
range (2):  
template <class InputIterator>
  map (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());
copy (3):   
map (const map& x);
map (const map& x, const allocator_type& alloc);
move (4):   
map (map&& x);
map (map&& x, const allocator_type& alloc);
initializer list (5):   
map (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());

定義程序示例

#include <iostream>
#include <map>

bool fncomp (char lhs, char rhs) {return lhs<rhs;}

struct classcomp {
  bool operator() (const char& lhs, const char& rhs) const
  {return lhs<rhs;}
};

int main ()
{
  std::map<char,int> first;

  first['a']=10;
  first['b']=30;
  first['c']=50;
  first['d']=70;

  std::map<char,int> second (first.begin(),first.end());

  std::map<char,int> third (second);

  std::map<char,int,classcomp> fourth;                 // class as Compare

  bool(*fn_pt)(char,char) = fncomp;
  std::map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare

  return 0;
}

基本操作

1、map::operator[]:

mapped_type& operator[] (const key_type& k);
mapped_type& operator[] (key_type&& k);
//如果k在map裏面存在,則返回映射值,如果不存在,就會以k值進本身行插入並返回一個相對映射值的引用(map會自動開闢一個空間以放置多餘的元素)。相同的類似的操作符map::at,除了k在不存在時會拋出異常,其他與[]操作符功能相同。

示例程序

#include <iostream>
#include <map>
#include <string>

int main ()
{
  std::map<char,std::string> mymap;

  mymap['a']="an element";
  mymap['b']="another element";
  mymap['c']=mymap['b'];

  std::cout << "mymap['a'] is " << mymap['a'] << '\n';
  std::cout << "mymap['b'] is " << mymap['b'] << '\n';
  std::cout << "mymap['c'] is " << mymap['c'] << '\n';
  std::cout << "mymap['d'] is " << mymap['d'] << '\n';
//注意最後一個元素‘d’的訪問操作時,操作器在map容器中插入了一個元素(初始化),一個空的字符串,僅僅可以通過檢索找到,通過map::find是找不到的

  std::cout << "mymap now contains " << mymap.size() << " elements.\n";

  return 0;
}

output:

mymap['a'] is an element
mymap['b'] is another element
mymap['c'] is another element
mymap['d'] is
mymap now contains 4 elements.

2、map::find

      iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
//在map容器中尋找是否有對應的標記的映射值,如果有的話就返回只想這個元素的指針,如果沒有就返回指向map::end的指針

示例程序

#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> mymap;
  std::map<char,int>::iterator it;

  mymap['a']=50;
  mymap['b']=100;
  mymap['c']=150;
  mymap['d']=200;

  it = mymap.find('b');
  if (it != mymap.end())
    mymap.erase (it);

  // print content:
  std::cout << "elements in mymap:" << '\n';
  std::cout << "a => " << mymap.find('a')->second << '\n';
  std::cout << "c => " << mymap.find('c')->second << '\n';
  std::cout << "d => " << mymap.find('d')->second << '\n';

  return 0;
}

3、map::begin:返回指向第一個元素的指針,如果不存在,返回的指針將不能被訪問。

4、map::end:返回指向最後一個元素的指針,如果不存在,返回的指針將不能被訪問。

示例程序

#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  // show content:
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

  return 0;
}

5、map::size:返回map容器中的元素數量

6、map::empty:如果map容器爲空,則返回1,反之返回0

發佈了99 篇原創文章 · 獲贊 194 · 訪問量 74萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章