{"id":137,"date":"2019-01-01T10:22:11","date_gmt":"2019-01-01T18:22:11","guid":{"rendered":"http:\/\/35.243.195.209\/?p=137"},"modified":"2019-01-01T10:22:11","modified_gmt":"2019-01-01T18:22:11","slug":"change-object-while-iterating-it","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/01\/01\/change-object-while-iterating-it\/","title":{"rendered":"Change Object While Iterating It"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post we will discuss how to change object while iterating it.<\/p>\n<h2>Details<\/h2>\n<p>Say we have a map.<\/p>\n<pre><code class=\"c\">unordered_map&lt;char, int&gt; char_counter ({{'1', 1}, {'a', 1}, {'b', 2}, {'c', 3}});\n<\/code><\/pre>\n<p>Now, we want to delete all elements whose value is 1. To delete an element using iterator we will use <code>erase()<\/code> function. But the problem is <strong>after erasing, this iterator &#8220;position&#8221; will become invalid. How can we get the next iterator?<\/strong><\/p>\n<p>The answer is <code>it = wordMap.erase(it);<\/code>.<\/p>\n<p>We provide the <strong>correct<\/strong> version first.<\/p>\n<pre><code class=\"c\">for (auto &amp;&amp; it = char_counter.begin(); it != char_counter.end();) {\n        cout &lt;&lt; (*it).first &lt;&lt; \":\" &lt;&lt; (*it).second &lt;&lt; endl;\n        if ((*it).second == 1) {\n                it = char_counter.erase(it);\n        } else {\n                it++;\n        }\n}\n<\/code><\/pre>\n<p>The following for each loop <strong>will not work<\/strong>.<\/p>\n<pre><code class=\"c\">for (const auto &amp; k_v : char_counter) {\n        if (k_v.second == 1) {\n                char_counter.erase(k_v.first);\n        }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post we will discuss how to change object while iterating it. Details Say we have a map. unordered_map&lt;char, int&gt; char_counter ({{&#8216;1&#8217;, 1}, {&#8216;a&#8217;, 1}, {&#8216;b&#8217;, 2}, {&#8216;c&#8217;, 3}}); Now, we want to delete all elements whose value is 1. To delete an element using iterator we will use erase() function. But the&#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-137","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\/137","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=137"}],"version-history":[{"count":3,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/137\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}