{"id":121,"date":"2008-11-13T23:51:10","date_gmt":"2008-11-13T14:21:10","guid":{"rendered":"http:\/\/www.clearchain.com\/blog\/?p=121"},"modified":"2011-02-07T11:24:48","modified_gmt":"2011-02-07T01:54:48","slug":"stlreverse-iterator","status":"publish","type":"post","link":"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator","title":{"rendered":"Stl::reverse iterator"},"content":{"rendered":"<p>I was coding a little program a while back when I found a <a title=\"Category:Programming Quirks\" href=\"http:\/\/www.clearchain.com\/blog\/categories\/computers\/programming\">Quirk<\/a> about the <code>stl  reverse_iterator<\/code>. Use of the iterator is quite easy until you have to delete from one. I tried the normal way of:<\/p>\n<pre>vector&lt;sometype&gt;::reverse_iterator myiterator...\r\n..\r\n..\r\nmyvector.erase(myiterator).<\/pre>\n<p>only to find the code didn&#8217;t compile. A little baffled I started to looking into why. It turns out you <strong>Can&#8217;t&#8217;<em> delete from a reverse iterator directly. So the question is <\/em>How do you delete from a reverse iterator?<\/strong>.<\/p>\n<p>Not wanting to do a full forward traversal to get to the same point I did some <a class=\"external text\" title=\"http:\/\/www.google.com\" rel=\"nofollow\" href=\"http:\/\/www.google.com\/\">Googling<\/a>. Eventually I found the page: <a class=\"external free\" title=\"http:\/\/www.ddj.com\/dept\/cpp\/184401406\" rel=\"nofollow\" href=\"http:\/\/www.ddj.com\/dept\/cpp\/184401406\">http:\/\/www.ddj.com\/dept\/cpp\/184401406<\/a><\/p>\n<p>which explains why you can&#8217;t delete from the reverse iterator. More importantly it indicates a way that you can delete from the reverse_iterator. So how?<\/p>\n<pre lang=\"c\"> void Manager::raiseWindow(WMWindow* wmwindow)\r\n {\r\n   <strong>vector&lt;WMWindow*&gt;::reverse_iterator it = windows.rbegin();<\/strong>\r\n   while(it\u00a0!= windows.rend())\r\n   {\r\n     if (*it == wmwindow)\r\n     {\r\n      \/\/ why ++it see <a class=\"external free\" title=\"http:\/\/www.ddj.com\/dept\/cpp\/184401406\" rel=\"nofollow\" href=\"http:\/\/www.ddj.com\/dept\/cpp\/184401406\">http:\/\/www.ddj.com\/dept\/cpp\/184401406<\/a>\r\n      <strong>windows.erase((++it).base());<\/strong>\r\n      windows.push_back(wmwindow);\r\n      break;\r\n     }\r\n     it++;\r\n   }\r\n\r\n   wmwindow->raise();\r\n }<\/pre>\n<p>Gives an example of how to do it. However, be aware, doing the above erase, invalidates the iterator so don&#8217;t go trying to use it again afterwards! }<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was coding a little program a while back when I found a Quirk about the stl reverse_iterator. Use of the iterator is quite easy until you have to delete from one. I tried the normal way of: vector&lt;sometype&gt;::reverse_iterator myiterator&#8230; .. .. myvector.erase(myiterator). only to find the code didn&#8217;t compile. A little baffled I started<a href=\"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator\"> <font size=-2>[..more..]<\/font><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[97,99,98],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-programming","tag-c","tag-iterator","tag-stl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Stl::reverse iterator - ClearChain<\/title>\n<meta name=\"description\" content=\"I was coding a little program a while back when I found a Quirk about the stl reverse_iterator. Use of the iterator is quite easy until you have to delete\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin Close\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator\"},\"author\":{\"name\":\"Benjamin Close\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98\"},\"headline\":\"Stl::reverse iterator\",\"datePublished\":\"2008-11-13T14:21:10+00:00\",\"dateModified\":\"2011-02-07T01:54:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator\"},\"wordCount\":164,\"commentCount\":0,\"keywords\":[\"c++\",\"iterator\",\"stl\"],\"articleSection\":[\"Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator\",\"url\":\"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator\",\"name\":\"Stl::reverse iterator - ClearChain\",\"isPartOf\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/#website\"},\"datePublished\":\"2008-11-13T14:21:10+00:00\",\"dateModified\":\"2011-02-07T01:54:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98\"},\"description\":\"I was coding a little program a while back when I found a Quirk about the stl reverse_iterator. Use of the iterator is quite easy until you have to delete\",\"breadcrumb\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.clearchain.com\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Stl::reverse iterator\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#website\",\"url\":\"https:\/\/www.clearchain.com\/blog\/\",\"name\":\"ClearChain\",\"description\":\"-= Daily Happenings =-\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.clearchain.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98\",\"name\":\"Benjamin Close\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg\",\"caption\":\"Benjamin Close\"},\"sameAs\":[\"http:\/\/www.clearchain.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Stl::reverse iterator - ClearChain","description":"I was coding a little program a while back when I found a Quirk about the stl reverse_iterator. Use of the iterator is quite easy until you have to delete","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator","twitter_misc":{"Written by":"Benjamin Close","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator#article","isPartOf":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator"},"author":{"name":"Benjamin Close","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98"},"headline":"Stl::reverse iterator","datePublished":"2008-11-13T14:21:10+00:00","dateModified":"2011-02-07T01:54:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator"},"wordCount":164,"commentCount":0,"keywords":["c++","iterator","stl"],"articleSection":["Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator","url":"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator","name":"Stl::reverse iterator - ClearChain","isPartOf":{"@id":"https:\/\/www.clearchain.com\/blog\/#website"},"datePublished":"2008-11-13T14:21:10+00:00","dateModified":"2011-02-07T01:54:48+00:00","author":{"@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98"},"description":"I was coding a little program a while back when I found a Quirk about the stl reverse_iterator. Use of the iterator is quite easy until you have to delete","breadcrumb":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.clearchain.com\/blog\/posts\/stlreverse-iterator#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.clearchain.com\/blog"},{"@type":"ListItem","position":2,"name":"Stl::reverse iterator"}]},{"@type":"WebSite","@id":"https:\/\/www.clearchain.com\/blog\/#website","url":"https:\/\/www.clearchain.com\/blog\/","name":"ClearChain","description":"-= Daily Happenings =-","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.clearchain.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98","name":"Benjamin Close","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg","caption":"Benjamin Close"},"sameAs":["http:\/\/www.clearchain.com"]}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":4,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":886,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/121\/revisions\/886"}],"wp:attachment":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}