{"id":2079,"date":"2017-03-26T12:37:54","date_gmt":"2017-03-26T17:37:54","guid":{"rendered":"https:\/\/bitpost.com\/news\/?p=2079"},"modified":"2017-03-26T12:41:21","modified_gmt":"2017-03-26T17:41:21","slug":"best-practice-to-bulk-remove-from-any-c-container","status":"publish","type":"post","link":"https:\/\/bitpost.com\/news\/2017\/best-practice-to-bulk-remove-from-any-c-container\/","title":{"rendered":"Best practice to bulk-remove from any C++ container"},"content":{"rendered":"<p>You can directly delete iterators while looping through associative containers like set and map:<\/p>\n<pre><code>\r\n    for(auto itue = usersByEmail_.begin(), itue_end = usersBySecret_.end(); itue != itue_end; ) \r\n    {\r\n        if ((*itue)->bDeleted())\r\n        { \r\n            \/\/ NOTE We could do more cleanup work here if needed.\r\n            itue = usersByEmail_.erase(itue);\r\n\r\n        } else ++itue;\r\n    }\r\n<\/code><\/pre>\n<p>With sequential containers like vector, you should use the erase-remove idiom.  Here&#8217;s an example for my <a href=\"https:\/\/bitpost.com\/wiki\/Major_Objects\">multi-indexed sets of pointers<\/a> that uses a lambda to keep the code tight.  runsByRank_ is of my sorted_vector type:<\/p>\n<pre><code>\r\n    runsByRank_.erase(\r\n        remove_if(\r\n            runsByRank_.begin(), runsByRank_.end(),    \r\n            []( StockRun*& psr )\r\n            { \r\n                \/\/ NOTE We could do more cleanup work here if needed.\r\n                return psr->bDeleted(); \r\n            }\r\n        ),\r\n        runsByRank_.end()\r\n    );\r\n<\/code><\/pre>\n<p>I&#8217;ll try to get my quick-http skeleton app updated with these best practices soon.  As of now it does not include any delayed-deletion pattern.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can directly delete iterators while looping through associative containers like set and map: for(auto itue = usersByEmail_.begin(), itue_end = usersBySecret_.end(); itue != itue_end; ) { if ((*itue)->bDeleted()) { \/\/ NOTE We could do more cleanup work here if needed. itue = usersByEmail_.erase(itue); } else ++itue; } With sequential containers like vector, you should use [&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,19,233],"tags":[],"class_list":["post-2079","post","type-post","status-publish","format-standard","hentry","category-cpp","category-opensource","category-quick-http"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9M11L-xx","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2079","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=2079"}],"version-history":[{"count":5,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2079\/revisions"}],"predecessor-version":[{"id":2085,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2079\/revisions\/2085"}],"wp:attachment":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/media?parent=2079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/categories?post=2079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/tags?post=2079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}