1. 程式人生 > 其它 >C++ Map常見用法說明

C++ Map常見用法說明

C++中map提供的是一種鍵值對容器,裡面的資料都是成對出現的,如下圖:每一對中的第一個值稱之為關鍵字(key),每個關鍵字只能在map中出現一次;第二個稱之為該關鍵字的對應值。

一. 宣告

//標頭檔案
#include<map>

map<int, string> ID_Name;

// 使用{}賦值是從c++11開始的,因此編譯器版本過低時會報錯,如visual studio 2012
map<int, string> ID_Name = { { 2015, "Jim" },{ 2016, "Tom" },{ 2017, "Bob" } };

二. 插入操作

2.1 使用[ ]進行單個插入

map<int, string> ID_Name;

// 如果已經存在鍵值2015,則會作賦值修改操作,如果沒有則插入
ID_Name[2015] = "Tom";

2.1 使用insert進行單個和多個插入

insert共有4個過載函式:

// 插入單個鍵值對,並返回插入位置和成功標誌,插入位置已經存在值時,插入失敗
pair<iterator,bool> insert (const value_type& val);

//在指定位置插入,在不同位置插入效率是不一樣的,因為涉及到重排
iterator insert (const_iterator position, const
value_type& val); // 插入多個 void insert (InputIterator first, InputIterator last); //c++11開始支援,使用列表插入多個 void insert (initializer_list<value_type> il);

具體使用示例:

 1 #include <iostream>
 2 #include <map>
 3 
 4 int main()
 5 {
 6     std::map<char, int> mymap;
 7 
 8     // 插入單個值
9 mymap.insert(std::pair<char, int>('a', 100)); 10 mymap.insert(std::pair<char, int>('z', 200)); 11 12 //返回插入位置以及是否插入成功 13 std::pair<std::map<char, int>::iterator, bool> ret; 14 ret = mymap.insert(std::pair<char, int>('z', 500)); 15 if (ret.second == false) { 16 std::cout << "element 'z' already existed"; 17 std::cout << " with a value of " << ret.first->second << '\n'; 18 } 19 20 //指定位置插入 21 std::map<char, int>::iterator it = mymap.begin(); 22 mymap.insert(it, std::pair<char, int>('b', 300)); //效率更高 23 mymap.insert(it, std::pair<char, int>('c', 400)); //效率非最高 24 25 //範圍多值插入 26 std::map<char, int> anothermap; 27 anothermap.insert(mymap.begin(), mymap.find('c')); 28 29 // 列表形式插入 30 anothermap.insert({ { 'd', 100 }, {'e', 200} }); 31 32 return 0; 33 }

三. 取值

Map中元素取值主要有at和[ ]兩種操作,at會作下標檢查,而[]不會。

map<int, string> ID_Name;

//ID_Name中沒有關鍵字2016,使用[]取值會導致插入
//因此,下面語句不會報錯,但列印結果為空
cout<<ID_Name[2016].c_str()<<endl;

//使用at會進行關鍵字檢查,因此下面語句會報錯
ID_Name.at(2016) = "Bob";

四. 容量查詢

// 查詢map是否為空
bool empty();

// 查詢map中鍵值對的數量
size_t size();

// 查詢map所能包含的最大鍵值對數量,和系統和應用庫有關。
// 此外,這並不意味著使用者一定可以存這麼多,很可能還沒達到就已經開闢記憶體失敗了
size_t max_size();

// 查詢關鍵字為key的元素的個數,在map裡結果非0即1
size_t count( const Key& key ) const; //

五. 迭代器

共有八個獲取迭代器的函式:* begin, end, rbegin,rend* 以及對應的 * cbegin, cend, crbegin,crend*。

二者的區別在於,後者一定返回 const_iterator,而前者則根據map的型別返回iterator 或者 const_iterator。const情況下,不允許對值進行修改。如下面程式碼所示:

map<int,int>::iterator it;
map<int,int> mmap;
const map<int,int> const_mmap;

it = mmap.begin(); //iterator
mmap.cbegin(); //const_iterator

const_mmap.begin(); //const_iterator
const_mmap.cbegin(); //const_iterator

六. 刪除交換

6.1 刪除

// 刪除迭代器指向位置的鍵值對,並返回一個指向下一元素的迭代器
iterator erase( iterator pos )

// 刪除一定範圍內的元素,並返回一個指向下一元素的迭代器
iterator erase( const_iterator first, const_iterator last );

// 根據Key來進行刪除, 返回刪除的元素數量,在map裡結果非0即1
size_t erase( const key_type& key );

// 清空map,清空後的size為0
void clear();

6.2 交換

// 就是兩個map的內容互換
void swap( map& other );

七. 順序比較

// 比較兩個關鍵字在map中位置的先後
key_compare key_comp() const;

示例:

map<char,int> mymap;
map<char,int>::key_compare mycomp = mymap.key_comp();

mymap['a']=100;
mymap['b']=200;
mycomp('a', 'b'); // a排在b前面,因此返回結果為true

八. 查詢

// 關鍵字查詢,找到則返回指向該關鍵字的迭代器,否則返回指向end的迭代器
// 根據map的型別,返回的迭代器為 iterator 或者 const_iterator
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;

舉例:

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); // b被成功刪除

九. 操作符

operator: == != < <= > >=
注意 對於==運算子, 只有鍵值對以及順序完全相等才算成立。


原文連結