關聯容器(Associative containers)支持通過鍵來高效地查找和讀取元素。兩個基本的關聯容器類型是 map 和set。map 的元素以鍵-值(key-value)對的形式組織:鍵用作元素在 map 中的索引,而值則表示所存儲和讀取的數據。set僅包含一個鍵,並有效地支持關於某個鍵是否存在的查詢。set 和 map 類型的對象所包含的元素都具有不同的鍵,不允許為同一個鍵添加第二個元素。如果一個鍵必須對應多個實例,則需使用 multimap 或 multiset,這兩種類型允許多個元素擁有相同的鍵。
map
關聯數組:元素通過鍵來存儲和讀取
set
大小可變的集合,支持通過鍵實現的快速讀取
multimap
支持同一個鍵多次出現的 map 類型
multiset
支持同一個鍵多次出現的 set 類型
pair 類型
1 string firstBook; 2 // access and test the data members of the pair 3 if (author.first == "James" && author.second == "Joyce") 4 firstBook = "Stephen Hero";
map 類型
map <string, int> word_count; // empty map // insert default initialzed element with key Anna; then assign 1 to its value word_count["Anna"] = 1;
對於 map 容器,如果下標所表示的鍵在容器中不存在,則添加新元素,這一特性可使程序驚人地簡練:
1 // count number of times each word occurs in the input 3 map<string, int> word_count; // empty map from string to int 5 string word; 6 7 while (cin >> word) 8 ++word_count[word];
1 // count number of times each word occurs in the input
2 map<string, int> word_count; // empty map from string to int
3 string word;
4
5 while (cin >> word) {
6 // inserts element with key equal to word and value 1;
7 // if word already in word_count, insert does nothing
8 pair<map<string, int>::iterator, bool> ret =
9 word_count.insert(make_pair(word, 1));
10
11 if (!ret.second)
12 // word already in word_count
13 ++ret.first->second; // increment counter
14 }
1 // get iterator positioned on the first element
2 map<string, int>::const_iterator
3 map_it = word_count.begin();
4
5 // for each element in the map
6 while (map_it != word_count.end()) {
7 // print the element key, value pairs
8 cout << map_it->first << " occurs "
9 << map_it->second << " times" << endl;
10 ++map_it; // increment iterator to denote the next element
11 }
set 類型
multimap 和 multiset 類型
1 // author we'll look for
2 string search_item("Alain de Botton");
3
4 // how many entries are there for this author
5 typedef multimap<string, string>::size_type sz_type;
6 sz_type entries = authors.count(search_item);
7 // get iterator to the first entry for this author
8 multimap<string,string>::iterator iter =
9 authors.find(search_item);
10
11 // loop through the number of entries there are for this autho
12 for (sz_type cnt = 0; cnt != entries; ++cnt, ++iter)
13 cout <<iter->second << endl; // print each title
1 // definitions of authors and search_item as above
2 // beg and end denote range of elements for this author
3 typedef multimap<string, string>::iterator authors_it;
4
5 authors_it beg = authors.lower_bound(search_item),
6 end = authors.upper_bound(search_item);
7
8 // loop through the number of entries there are for this author
9 while (beg != end)
10 {
11 cout << beg->second << endl; // print each title
12 ++beg;
13 }
// definitions of authors and search_item as above
// pos holds iterators that denote range of elements for this key
pair<authors_it, authors_it>
pos = authors.equal_range(search_item);
// loop through the number of entries there are for this author
while (pos.first != pos.second)
{
cout << pos.first->second << endl; // print each title
++pos.first;
}