C++ Primer Plus 学习笔记 第十六章 关联容器

- 日理万妓 2022-01-11 05:19 393阅读 0赞

字典

数据结构为树

元素查找比链表快

4种关联容器

set, multiset,

map, multimap,

set:

键就是值 唯一

multiset 不唯一。可以有多个重复的键值

可翻转,可排序

格式: set A;

可以显示的指定排序的函数或对象 默认用less<> :

set> A;

set有将迭代器区间作为参数的构造函数

20190619172801939.png

只有1个for会写到A中

然后自动排序

可用于数学的集合运算(并集,交集,差集等)

在做这些运算前必须先将容器元素进行排序

set_union()

2019061917325154.png

20190619173255969.png

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvdWx3eWI_size_16_color_FFFFFF_t_70

set_intersection():交集

set_difference()差集

lower_bound() 传入的1个参数,查找容器中第一个不小于该参数的键的成员

upper_bound()传入的1个参数,查找容器中第一个大于该参数的键的成员

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvdWx3eWI_size_16_color_FFFFFF_t_70 1

set程序示例

  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. #include <algorithm>
  5. #include <iterator>
  6. int main()
  7. {
  8. using namespace std;
  9. const int N = 6;
  10. string s1[N] = {"buffoon", "thinkers", "for", "heavy", "can", "for"};
  11. string s2[N] = {"metal", "any", "food", "elegant", "deliver", "for"};
  12. set<string> A(s1, s1 + N);
  13. set<string> B(s2, s2 + N);
  14. ostream_iterator<string, char> out(cout, " ");
  15. cout << "Set A: ";
  16. copy(A.begin(), A.end(), out);
  17. cout << endl;
  18. cout << "Set B: ";
  19. copy(B.begin(), B.end(), out);
  20. cout <<endl;
  21. cout << "Union of A and B:\n";
  22. // 合并输出 并集
  23. set_union(A.begin(), A.end(), B.begin(), B.end(), out);
  24. cout << endl;
  25. cout << "Intersection of A and B:\n";
  26. // 交集输出
  27. set_intersection(A.begin(), A.end(), B.begin(), B.end(), out);
  28. cout << endl;
  29. cout << "Difference of A and B:\n";
  30. // 差集输出
  31. set_difference(A.begin(), A.end(), B.begin(), B.end(), out);
  32. cout << endl;
  33. set<string> C;
  34. cout << "Set C:\n";
  35. // 并集输出到C容器中 利用插入迭代器
  36. set_union(A.begin(), A.end(), B.begin(), B.end(),
  37. insert_iterator<set<string>> (C, C.begin()));
  38. copy(C.begin(), C.end(), out);
  39. cout << endl;
  40. string s3("grungy");
  41. // 插入并自动排序
  42. C.insert(s3);
  43. cout << "Set C after insertion:\n";
  44. copy(C.begin(), C.end(), out);
  45. cout << endl;
  46. cout << "Showing a range:\n";
  47. //string区间输出
  48. copy(C.lower_bound("ghost"), C.upper_bound("soppk"), out);
  49. cout << endl;
  50. return 0;
  51. }

程序结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvdWx3eWI_size_16_color_FFFFFF_t_70 2

map:

键与值不同,键唯一 一对一

multimap是一对多

元素也是一个STL模板

数据是按键排序

对于元素。可以使用first和second成员来访问键和值

count()传入键,获取键对应的值得数目

lower_bound() ,upper_bound() 使用与set一致

qual_range() 用键作为参数 返回符合该键的元素的两个迭代器区间,这个迭代器的类型就是multimap模板的类型

程序示例

  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <algorithm>
  5. typedef int KeyType;
  6. typedef std::pair<const KeyType, std::string> Pair;
  7. typedef std::multimap<KeyType, std::string> MapCode;
  8. int main()
  9. {
  10. using namespace std;
  11. MapCode codes;
  12. codes.insert(Pair(415, "San Franciso"));
  13. codes.insert(Pair(510, "Oakland"));
  14. codes.insert(Pair(718, "Brooklyn"));
  15. codes.insert(Pair(718, "Staten Island"));
  16. codes.insert(Pair(718, "Staten Island"));
  17. codes.insert(Pair(718, "Staten Island"));
  18. codes.insert(Pair(415, "San Rafael"));
  19. codes.insert(Pair(450, "Berkeley"));
  20. cout << "Number of cities with area code 415: "
  21. // 计数 键为415的元素数量
  22. << codes.count(415) << endl;
  23. cout << "Number of cities with area code 718: "
  24. << codes.count(415) << endl;
  25. cout << "Number of cities with area code 510: "
  26. << codes.count(415) << endl;
  27. cout << "Area Code City\n";
  28. MapCode::iterator it;
  29. for (it=codes.begin(); it != codes.end(); ++it)
  30. cout <<" " << (*it).first << " "
  31. << (*it).second << endl;
  32. 取得键为718multimap类型迭代器区间。(queal_range会将该迭代器区间包裹成pair类型模板)
  33. // 第一个迭代器表示起始位, 第二个迭代器表示超尾
  34. pair<MapCode::iterator, MapCode::iterator> range
  35. = codes.equal_range(718);
  36. cout << "Cities with area code 718:\n";
  37. for (it = range.first; it != range.second; ++it)
  38. {
  39. cout << (*it).second << endl;
  40. }
  41. return 0;
  42. }

无序关联容器 底层是用哈希表

unordered_set, unordered_multiset, unordered_map unordered_multimap

书上不说了 直接让我去看附录= =那我最后再去看吧

泛型编程部分 完结

发表评论

表情:
评论列表 (有 0 条评论,393人围观)

还没有评论,来说两句吧...

相关阅读