标签:
关联容器(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<K,V>::key_type | 在 map 容器中,用做索引的键的类型 |
map<K,V>::mapped_type | 在 map 容器中,键所关联的值的类型 |
map<K,V>::value_type | 一个 pair 类型,它的 first 元素具有 const map<K,V>::key_type 类型,而 second 元素则为 map<K,V>::mapped_type 类型 |
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; }
标签:
原文地址:http://www.cnblogs.com/huashu/p/4337858.html