注: 以下使用的接口均爲部分接口。
1. 序列式容器
回顧:vector,list,dequeue,等都被稱爲序列式容器,因爲其底層都是線性序列的數據結構,裏面存儲的是元素本身。
2.關聯式容器
關聯式容器也是用來存儲數據的,它與序列式容器不同的是,其裏面存儲的是<key, value>結構的鍵值對,在數據檢索時比序列式容器效率更高。
3. 鍵值對
用來表示具有一一對應關係的一種結構,該結構中一般只包含兩個成員變量key和value, key代表鍵值,value表示與key對應的關係。 舉個例子:現在要建立一個英漢互譯字典,那該字典中必然有英文單詞與其對應的中文含義。而且,英文單詞與其中文含義是一一對應的關係,即通過該單詞,在詞典中就可以找到與其對應的中文含義。
以下是關於鍵值對的定義:
template <class T1, class T2>
struct pair
{
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair()
:first(T1());
, second(T2());
{}
pair(const T1& a, const T2& b)
:first(a)
, second(b)
{}
};
4.樹形結構的關聯式容器
根據應用場景不同,STL總共實現了兩種不同結構的管理式容器:樹形結構和哈希結構。樹形關聯式容器主要有四種:map, set, multimap, multiset.這四種容器共同特點是:使用平衡搜索樹(紅黑樹)作爲其底層結構,容器中的元素是一個有序的序列。
(1)map:
a. map是關聯式容器,它按照特定的次序(按照key來比較)存儲由鍵值key和值value組合而成的元素。
b. 在map中,鍵值key通常用於排序和唯一標識元素,而value中存儲於此值關聯的內容
c. 在內部,map中的元素總是按照鍵值key進行比較排序的。
d.map支持下標訪問符,即在[]中放入key,就可以找到與key對應的value。
應用:
//map的構造
#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>
using namespace std;
void TestMap()
{
//key和value都給成字符串
map<string, string> m1;
map<string, string> m2{ { "apple", "蘋果" }, { "banana", "香蕉" }, { "orange", "橘子" },
{ "peach", "桃子" }, { "watermelon", "西瓜" } };
cout << m2["apple"] << endl;
cout << m2["watermelon"] << endl;
map<string, string> m3(m2);
}
int main()
{
TestMap();
system("pause");
return 0;
}
//map的迭代器
#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>
using namespace std;
void TestMap()
{
map<string, string> m{ { "apple", "蘋果" }, { "banana", "香蕉" }, { "orange", "橘子" },
{ "peach", "桃子" }, { "watermelon", "西瓜" } };
for (auto it = m.begin(); it != m.end(); ++it)
cout << (*it).first << "--->" << it->second << endl;
}
int main()
{
TestMap();
system("pause");
return 0;
}
//map的容量與元素訪問
#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>
using namespace std;
void TestMap()
{
//構造一個空的map,此時m中一個元素也沒有
map<string, string> m;
/*operator[]原理: 用<key, T()>構造一個鍵值對,然後調用insert()函數將鍵值對插入到map中
如果key已經存在,插入失敗,insert()函數返回該key所在位置的迭代器
如果key不存在,插入成功,insert()函數返回新插入元素所在位置的迭代器
operator[]函數最後將insert()返回值的鍵值對的value返回
*/
m["apple"] = "蘋果";
//將<"apple", "">插入到map中,插入失敗,將<"apple", "蘋果">中的“蘋果”返回
cout << m["apple"] << endl;
cout << m.size() << endl;
//banana不在map中,該函數拋異常
//m.at("banana");
}
int main()
{
TestMap();
system("pause");
return 0;
}
#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>
using namespace std;
void TestMap()
{
//map中的元素修改
map<string, string> m;
//向map中插入元素的方式;
//將鍵值對<"peach","桃子">插入到map中,用pair直接來構造鍵值對
m.insert(pair<string, string>("peach", "桃子"));
//將鍵值對<"peach","桃子">插入到map中,用make_pair直接來構造鍵值對
m.insert(make_pair("banana", "香蕉"));
//借用operator[]向map中插入元素
m["apple"] = "蘋果";
//key不存在時拋異常
//m.at("watermelon") = "西瓜";
m.insert(m.find("banana"), make_pair("watermelon", "西瓜"));
cout << m.size() << endl;
//用迭代器遍歷map中的元素,可以得到一個按照key排序的序列
for (auto& e : m)
cout << e.first << "--->" << e.second << endl;
cout << endl;
//map中的鍵值對key一定是唯一的,如果存在key則插入失敗
auto ret = m.insert(make_pair("peach","桃子"));
if (ret.second)
cout << "<peach, 桃子>不在map中,已經插入" << endl;
//刪除key爲"apple"的元素
m.erase("apple");
for (auto&e : m)
cout << e.first << "--->" << e.second << endl;
if (1 == m.count("apple"))
cout << "apple還在" << endl;
else
cout << "apple被吃了" << endl;
}
int main()
{
TestMap();
system("pause");
return 0;
}
【總結】
- map中的元素是鍵值對;
- map中的key是唯一的,並且不能修改
- 默認按照從小到大的順序對key進行排列
- map中的元素如果用迭代器去遍歷,可得到一個有序的序列
- 底層爲平衡搜索樹(紅黑樹),查找效率高(O(logN))
- 支持operator[],實際上爲插入查找。
(2)multimap
a. multimaps是關聯式容器,它按照特定的順序,存儲由key和value映射的鍵值對,其中多個鍵值對之間的key是可以重複的。
b. 在multimaps裏,通常按照key排序和唯一標識元素,而映射的value存儲與key關聯的內容。兩者的類型可以不同,通過內部成員value_type組合在一起。
c. 在內部,multimaps中的元素總是通過其內部比較對象,按照特定的順序標準對key進行排序。
d. 使用迭代器直接遍歷multimap中的元素可以得到關於key的有序序列。
e. 底層用二叉搜索樹(紅黑樹)實現。
注意:multimap和map的唯一不同是,map中的key是唯一的,而multimap中key可以重複。
應用:
#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>
using namespace std;
void test_multimap1()
{
multimap<string, string> m;
m.insert(make_pair("李逵", "黑旋風"));
m.insert(make_pair("林沖", "豹子頭"));
m.insert(make_pair("魯達", "花和尚"));
//嘗試插入key相同的元素
m.insert(make_pair("李逵", "鐵牛"));
cout << m.size() << endl;
for (auto& e : m)
{
cout << "<" << e.first << "," << e.second << ">" << endl;
}
//key爲李逵的元素有幾個
cout << "李逵:" << m.count("李逵") << endl;
}
int main()
{
test_multimap1();
system("pause");
return 0;
}
#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>
using namespace std;
void test_multimap2()
{
multimap<int, int> m;
for (int i = 0; i < 10; ++i)
m.insert(pair<int, int>(i, i));
for (auto&e : m)
cout << e.first << "--->" << e.second << endl;
cout << endl;
//返回m中大於等於5的第一個元素
auto it = m.lower_bound(5);
cout << it->first << "--->" << it->second << endl;
//返回m中大於5的第一個元素
it = m.upper_bound(5);
cout << it->first << "--->" << it->second << endl;
}
int main()
{
test_multimap2();
system("pause");
return 0;
}
(3)set
a. set是按照一定次序存儲元素的容器
b. 在set中,元素的value也標識它(value就是key,類型爲T),並且每個value必須是唯一的。set中的元素不能在容器中修改(元素總是const),但是可以從容器中插入或刪除它們
c. 在內部,set中的元素總是按照其內部比較對象(類型比較)所指示的特定嚴格弱排序準則進行排序
d. set底層是用二叉搜索樹(紅黑樹)實現
使用:
#include <string>
#include <set>
#include <iostream>
#include <stdlib.h>
using namespace std;
void test_set()
{
//用數組中的元素構造set
int array[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0, 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
set<int> s(array, array + sizeof(array)/sizeof(array[0]));
cout << s.size() << endl;
//正向打印set中的元素,從打印結果可以看出:set可以去重
for (auto& e : s)
cout << e << " ";
cout << endl;
//使用迭代器逆向打印set中的元素
for (auto it = s.rbegin(); it != s.rend(); ++it)
cout << *it << " ";
cout << endl;
//set中值爲3的元素出現了幾次
cout << s.count(3) << endl;
}
int main()
{
test_set();
system("pause");
return 0;
}
(4)multiset
a. multiset是按照特定順序存儲元素的容器,其中元素是可以重複的
b. 在multiset中,元素的value也會識別它(因爲multiset中本身存儲的就是<value,value>組成的鍵值對,因此value本身就是key,key就是value,類型爲T)multiset元素的值不能在容器中進行修改(因爲元素總是const的),但可以從容器中插入或刪除
c. 在內部,multiset中的元素總是按照其內部比較規則(類型比較)所指示的特定嚴格弱排序準則進行排序
d. 使用迭代器遍歷時會得到一個有序序列
e. multiset底層結構爲二叉搜索樹(紅黑樹)
使用:
#include <string>
#include <set>
#include <iostream>
#include <stdlib.h>
using namespace std;
void test_multiset()
{
int arr[] = { 2, 1, 3, 9, 6, 0, 5, 8, 4, 7 };
//注意:multiset在底層實際存儲的是<int, int>的鍵值對
multiset<int> s(arr, arr + sizeof(arr) / sizeof(arr[0]));
for (auto& e : s)
cout << e << " ";
cout << endl;
//測試multiset中是否可以存儲鍵值相同的元素
s.insert(5);
cout << s.count(5) << endl;
for (auto& e : s)
cout << e << " ";
cout << endl;
//刪除所有值爲5的元素
s.erase(5);
for (auto& e : s)
cout << e << " ";
cout << endl;
}
int main()
{
test_multiset();
system("pause");
return 0;