{"id":51,"date":"2018-12-21T00:00:20","date_gmt":"2018-12-21T03:00:20","guid":{"rendered":"http:\/\/35.243.195.209\/?p=51"},"modified":"2019-06-30T20:40:09","modified_gmt":"2019-07-01T03:40:09","slug":"ordered-containers","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2018\/12\/21\/ordered-containers\/","title":{"rendered":"Ordered Containers"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce the <code>map<\/code> and <code>priority_queue<\/code> in c++ STL.<\/p>\n<h2>Why Ordered Containers<\/h2>\n<p>Unlike the easy version <code>unordered_map<\/code>, in <code>map<\/code>, key values are generally used to sort. Thanks to the binary search tree, <code>map<\/code> has good performance in search and erase elements $(log(n))$.<\/p>\n<h2>Constructor<\/h2>\n<p>The following codes are self-explained.<\/p>\n<pre><code> \n\/\/ Example of usage of priority_queue\n\/\/ override < is a easy solution\nstruct T {\n        int t, x, y;\n        T(int a, int b, int c) : t (a), x (b), y (c){}\n        bool operator< (const T &#038;d) const {return t > d.t;}\n    };\n\nvoid temp(vector&lt;vector&lt;int&gt;&gt; & grid) {\n        int N = grid.size (), res = 0;\n        priority_queue<T> pq;\n        pq.push(T(grid[0][0], 0, 0));\n}\n<\/code><\/pre>\n<pre><code>#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;set&gt;\n\nclass Compare {\npublic:\n    bool operator() (int const & a, int const &b) const {\n        return a > b;\n    }\n};\n\nint main(){\n    \/\/ class based\n    Compare compare_instance;\n    \/\/ lambda based\n    auto comp = [](int i, int j){return i &gt; j;};\n    map&lt;int, std::string, decltype(comp)&gt; int2Str(comp);\n    \/\/ map&lt;int, std::string, Compare&gt; int2Str(compare_instance);\n    multiset&lt;int, decltype(comp)&gt; int_set(comp);\n    unordered_map&lt;int, string&gt; int2str_no_order = {{5, \"five\"}, {1, \"one\"}, {4, \"four\"}, {3, \"three\"},\n            {2, \"two\"}, {7, \"seven\"}, {6, \"six\"} };\n    for (auto k_v : int2str_no_order) {\n        int2Str[k_v.first] = k_v.second;\n        int_set.insert(k_v.first);\n    }\n\n    \/\/ can repeat\n    int2Str.insert(make_pair(7, \"seven\"));\n    int_set.insert(7);\n    for (auto p : int2Str) {\n        cout &lt;&lt; \"{\" &lt;&lt; p.first &lt;&lt; \",\" &lt;&lt; p.second &lt;&lt; \"} \";\n    }\n    \/\/ {7,seven} {6,six} {5,five} {4,four} {3,three} {2,two} {1,one}\n    cout &lt;&lt; endl;\n    for (auto p : int_set) {\n        cout &lt;&lt; p &lt;&lt; \" \";\n    }\n    \/\/ 7 7 6 5 4 3 2 1\n    return 0;\n}<\/code><\/pre>\n<h2>Search Funcitons<\/h2>\n<p>Ordered containers are optimized for searching in $(log(n))$.<\/p>\n<table>\n<thead>\n<tr>\n<th>Functions<\/th>\n<th><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ordAssCont.count(key)<\/td>\n<td>Returns the number of values with the key.<\/td>\n<\/tr>\n<tr>\n<td>ordAssCont.find(key)<\/td>\n<td>Returns the iterator of key in ordAssCont. If there is no key in ordAssCont it returns ordAssCont.end().<\/td>\n<\/tr>\n<tr>\n<td>ordAssCont.lower_bound(key)<\/td>\n<td>Returns the iterator to the first key in ordAssCont in which key would be inserted.<\/td>\n<\/tr>\n<tr>\n<td>ordAssCont.upper_bound(key)<\/td>\n<td>Returns the iterator to the last position of key in ordAssCont in which key would be inserted.<\/td>\n<\/tr>\n<tr>\n<td>ordAssCont.equal_range(key)<\/td>\n<td>Returns the range ordAssCont.lower bound(key) and ordAssCont.upper_bound(key) in a std::pair.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The following codes are self-explained.<\/p>\n<pre><code>#include &lt;iostream&gt;\n#include &lt;set&gt;\n\nint main(){\n  std::multiset&lt;int&gt; mySet{3, 1, 5, 3, 4, 5, 1, 4, 4, 3, 2, 2, 7, 6, 4, 3, 6};\n\n  for (auto s: mySet) std::cout &lt;&lt; s &lt;&lt; \" \"; \/\/ 1 1 2 2 3 3 3 3 4 4 4 4 5 5 6 6 7\n  std::cout&lt;&lt;\"\\n\";\n\n  mySet.erase(mySet.lower_bound(4), mySet.upper_bound(4));\n  for (auto s: mySet) std::cout &lt;&lt; s &lt;&lt; \" \"; \/\/ 1 1 2 2 3 3 3 3 5 5 6 6 7\n  std::cout&lt;&lt;\"\\n\";\n\n  std::cout &lt;&lt; mySet.count(3) &lt;&lt; std::endl; \/\/ 4\n  std::cout &lt;&lt; *mySet.find(3) &lt;&lt; std::endl; \/\/ 3\n  std::cout &lt;&lt; *mySet.lower_bound(3) &lt;&lt; std::endl; \/\/ 3\n  std::cout &lt;&lt; *mySet.upper_bound(3) &lt;&lt; std::endl; \/\/ 5\n  auto pair= mySet.equal_range(3);\n  std::cout &lt;&lt; \"(\" &lt;&lt; *pair.first &lt;&lt; \",\" &lt;&lt; *pair.second &lt;&lt; \")\"; \/\/ (3,5)\n\n  return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce the map and priority_queue in c++ STL. Why Ordered Containers Unlike the easy version unordered_map, in map, key values are generally used to sort. Thanks to the binary search tree, map has good performance in search and erase elements $(log(n))$. Constructor The following codes are self-explained. \/\/ Example&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":10,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":485,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/51\/revisions\/485"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}