C++之Map

  看到一篇介紹map很不錯的文章,轉載一下,感謝原作者的整理。轉載地址:http://blog.csdn.net/shuzfan/article/details/53115922

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

http://www.studytonight.com/cpp/images/map-example.png

——————————————————————————————————————————————

一. 聲明

//頭文件
#include<map>

map<int, string> ID_Name;

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

——————————————————————————————————————————————

二. 插入操作

2.1 使用[ ]進行單個插入

map<int, string> ID_Name;

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

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

下面是具體使用示例:

#include <iostream>
#include <map>

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

    // 插入單個值
    mymap.insert(std::pair<char, int>('a', 100));
    mymap.insert(std::pair<char, int>('z', 200));

    //返回插入位置以及是否插入成功
    std::pair<std::map<char, int>::iterator, bool> ret;
    ret = mymap.insert(std::pair<char, int>('z', 500));
    if (ret.second == false) {
        std::cout << "element 'z' already existed";
        std::cout << " with a value of " << ret.first->second << '\n';
    }

    //指定位置插入
    std::map<char, int>::iterator it = mymap.begin();
    mymap.insert(it, std::pair<char, int>('b', 300));  //效率更高
    mymap.insert(it, std::pair<char, int>('c', 400));  //效率非最高

    //範圍多值插入
    std::map<char, int> anothermap;
    anothermap.insert(mymap.begin(), mymap.find('c'));

    // 列表形式插入
    anothermap.insert({ { 'd', 100 }, {'e', 200} });

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 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";
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

——————————————————————————————————————————————

四. 容量查詢

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

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

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

// 查詢關鍵字爲key的元素的個數,在map裏結果非0即1
size_t count( const Key& key ) const; //
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

——————————————————————————————————————————————

五. 迭代器

共有八個獲取迭代器的函數:* 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

返回的迭代器可以進行加減操作,此外,如果map爲空,則 begin = end。

這裏寫圖片描述

——————————————————————————————————————————————

六. 刪除交換

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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

6.2 交換

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

——————————————————————————————————————————————

七. 順序比較

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

示例:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

——————————————————————————————————————————————

八. 查找

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

舉例:

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被成功刪除
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

——————————————————————————————————————————————

九. 操作符

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


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