{"id":1621,"date":"2013-06-07T20:19:36","date_gmt":"2013-06-08T01:19:36","guid":{"rendered":"http:\/\/bitpost.com\/news\/?p=1621"},"modified":"2013-11-06T15:48:36","modified_gmt":"2013-11-06T20:48:36","slug":"quick-c11-containers-summary","status":"publish","type":"post","link":"https:\/\/bitpost.com\/news\/2013\/quick-c11-containers-summary\/","title":{"rendered":"Quick C++11 containers summary"},"content":{"rendered":"<table class=\"mw-collapsible mw-collapsed wikitable\" border=2 cellpadding=10>\n<tr>\n<th colspan=2> c++11 containers\n<\/th>\n<\/tr>\n<tr>\n<td> <a href=\"http:\/\/bitpost.com\/news\/2011\/01\/21\/cross-platform-c-sorted_vector-derived-from-stdvector\/\" title=\"sorted_vector\">sorted_vector<\/a> <\/td>\n<td> use when doing lots of unsorted insertions and maintaining constant sort would be expensive\n<\/td>\n<\/tr>\n<tr>\n<td> map <\/td>\n<td> sorted binary search tree; always sorted by key; you can walk through in sorted order\n<\/td>\n<\/tr>\n<tr>\n<td> multimap <\/td>\n<td> same as map but allows dupe keys; need for this should be pretty rare\n<\/td>\n<\/tr>\n<tr>\n<td> unordered_map <\/td>\n<td> hashmap; always sorted by key; additional bucket required for hash collisions; no defined order when walking through\n<\/td>\n<\/tr>\n<tr>\n<td> unordered_multimap <\/td>\n<td> same as map but allows dupe keys; dupes are obviously in the same bucket, and you can walk just the dupes if needed\n<\/td>\n<\/tr>\n<tr>\n<td> set<br \/>multiset<br \/>unordered_set<br \/>unordered_multiset <\/td>\n<td>sets are just like maps, except the key is embedded in the object, great idea<br \/>\nbut&#8230; they are ruined by the constraint that contained items must be const<br \/>\nbut&#8230; then redeemed by mutable<br \/>\nYou can use mutable on the variable that are not part of the key to remove the const!<br \/>\nThis changes the constness of the object from binary (completely const) to logical (constness is defined by the developer)<br \/>\nso&#8230; set is an excellent way to achieve both encapsulation and logical const<\/p>\n<p>Final note: using object pointers is the ultimate solution.<br \/>\nThe entire object can be dereferenced and accessed then without const issues.<br \/>\nTwo requirements: you must make sure yourself that you do not change the key values;<br \/>\nyou must create sort functors that dereference the pointers to sort using object contents if needed<br \/>\n(the default sorting will be by pointer address).<br \/>\nThe arguably biggest advantage, as a result, is that you can create multiple sets<br \/>\nto reference the same group of objects with different sort funtors to create multiple indices.<br \/>\nOH YEAH!  \ud83d\ude42\n<\/td>\n<\/tr>\n<\/table>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>c++11 containers sorted_vector use when doing lots of unsorted insertions and maintaining constant sort would be expensive map sorted binary search tree; always sorted by key; you can walk through in sorted order multimap same as map but allows dupe keys; need for this should be pretty rare unordered_map hashmap; always sorted by key; additional [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[22],"tags":[190,30],"class_list":["post-1621","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-c11","tag-stl"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9M11L-q9","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/1621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/comments?post=1621"}],"version-history":[{"count":9,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/1621\/revisions"}],"predecessor-version":[{"id":1737,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/1621\/revisions\/1737"}],"wp:attachment":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/media?parent=1621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/categories?post=1621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/tags?post=1621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}