map是STL的一個關聯容器,它提供一對一的hash。
- 第一個可以稱為關鍵字(key),每個關鍵字只能在map中出現一次;
- 第二個可能稱為該關鍵字的值(value);
map以模板(泛型)方式實現,可以存儲任意類型的數據,包括使用者自定義的數據類型。Map主要用於資料一對一映射(one-to-one)的情況,map內部的實現自建一顆紅黑樹,這顆樹具有對數據自動排序的功能。在map內部所有的數據都是有序的,后邊我們會見識到有序的好處。比如一個班級中,每個學生的學號跟他的姓名就存在著一對一映射的關系。
2,map的功能
自動建立key - value的對應。key 和 value可以是任意你需要的類型,包括自定義類型。
3,使用map
使用map得包含map類所在的頭文件
#include <map> //注意,STL頭文件沒有擴展名.h
map對象是模板類,需要關鍵字和存儲對象兩個模板參數:
std:map<int, string> personnel;
這樣就定義了一個用int作為索引,並擁有相關聯的指向string的指針.
為了使用方便,可以對模板類進行一下類型定義,
typedef map<int,CString> UDT_MAP_INT_CSTRING;
UDT_MAP_INT_CSTRING enumMap;
4,map的構造函數
map共提供了6個構造函數,這塊涉及到內存分配器這些東西,略過不表,在下面我們將接觸到一些map的構造方法,這里要說下的就是,我們通常用如下方法構造一個map:
map<int, string> mapStudent;
map<string , int >mapstring; map<int ,string >mapint;
map<sring, char>mapstring; map< char ,string>mapchar;
map<char ,int>mapchar; map<int ,char >mapint;
5,插入元素
// 定義一個map對象
map<int, string> mapStudent;
// 第一種 用insert函數插入pair
mapStudent.insert(pair<int, string>(000, "student_zero"));
// 第二種 用insert函數插入value_type數據
mapStudent.insert(map<int, string>::value_type(001, "student_one"));
// 第三種 用"array"方式插入
mapStudent[123] = "student_first";
mapStudent[456] = "student_second";
以上三種用法,雖然都可以實現數據的插入,但是它們是有區別的,當然了第一種和第二種在效果上是完成一樣的,用insert函數插入數據,在數據的 插入上涉及到集合的唯一性這個概念,即當map中有這個關鍵字時,insert操作是不能在插入數據的,但是用數組方式就不同了,它可以覆蓋以前該關鍵字對 應的值,用程序說明如下:
mapStudent.insert(map<int, string>::value_type (001, "student_one"));
mapStudent.insert(map<int, string>::value_type (001, "student_two"));
上面這兩條語句執行后,map中001這個關鍵字對應的值是“student_one”,第二條語句並沒有生效,那么這就涉及到我們怎么知道insert語句是否插入成功的問題了,可以用pair來獲得是否插入成功,程序如下
// 構造定義,返回一個pair對象
pair<iterator,bool> insert (const value_type& val);
pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(map<int, string>::value_type (001, "student_one"));
if(!Insert_Pair.second)
cout << ""Error insert new element" << endl;
我們通過pair的第二個變量來知道是否插入成功,它的第一個變量返回的是一個map的迭代器,如果插入成功的話Insert_Pair.second應該是true的,否則為false。
6, 查找元素
當所查找的關鍵key出現時,它返回數據所在對象的位置,如果沒有,返回iter與end函數的值相同。
// find 返回迭代器指向當前查找元素的位置否則返回map::end()位置
iter = mapStudent.find("123");
if(iter != mapStudent.end())
cout<<"Find, the value is"<<iter->second<<endl;
else
cout<<"Do not Find"<<endl;
7, 刪除與清空元素
//迭代器刪除
iter = mapStudent.find("123");
mapStudent.erase(iter);
//用關鍵字刪除
int n = mapStudent.erase("123"); //如果刪除了會返回1,否則返回0
//用迭代器范圍刪除 : 把整個map清空
mapStudent.erase(mapStudent.begin(), mapStudent.end());
//等同於mapStudent.clear()
8,map的大小
在往map里面插入了數據,我們怎么知道當前已經插入了多少數據呢,可以用size函數,用法如下:
int nSize = mapStudent.size();
9,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的函數
二. 插入操作
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);
下面是具體使用示例:
#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;
}
三. 取值
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
返回的迭代器可以進行加減操作,此外,如果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();
6.2 交換
// 就是兩個map的內容互換
void swap( map& other );
Map中的swap不是一個容器中的元素交換,而是兩個容器交換;———————
6.map的sort問題:
Map中的元素是自動按key升序排序,所以不能對map用sort函數
七. 順序比較
// 比較兩個關鍵字在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: == != < <= > >=
注意 對於==運算符, 只有鍵值對以及順序完全相等才算成立。
http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html
https://www.cnblogs.com/yonglin1998/p/11780828.html
二. 插入操作
2.1 使用[ ]進行單個插入
map<int, string> ID_Name;
// 如果已經存在鍵值2015,則會作賦值修改操作,如果沒有則插入
ID_Name[2015] = "Tom";
- 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
下面是具體使用示例:
#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
——————————————————————————————————————————————
三. 取值
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
——————————————————————————————————————————————
四. 容量查詢
// 查詢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
——————————————————————————————————————————————
五. 迭代器
共有八個獲取迭代器的函數:* 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
返回的迭代器可以進行加減操作,此外,如果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
6.2 交換
// 就是兩個map的內容互換
void swap( map& other );
- 1
- 2
——————————————————————————————————————————————
七. 順序比較
// 比較兩個關鍵字在map中位置的先后
key_compare key_comp() const;
- 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
——————————————————————————————————————————————
八. 查找
// 關鍵字查詢,找到則返回指向該關鍵字的迭代器,否則返回指向end的迭代器
// 根據map的類型,返回的迭代器為 iterator 或者 const_iterator
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
- 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
——————————————————————————————————————————————
九. 操作符
operator: == != < <= > >=
注意 對於==運算符, 只有鍵值對以及順序完全相等才算成立。