title: How to decrement iterator in C++?
body: <p>I am using c++ stl list of custom defined structs and I have an iterator that goes through that list but at one point I need to decrement and go back a few entries in the list. I tried itt-- but that just gave me a segmentation fault. Is there any other way of doing it.</p>
  
  <p>I also tried this <code>std::advance(cur_instruc, a);</code></p>
  
  <p>Here is what the iterator looks like:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;list&gt;
  
  using namespace std;
  
  typedef struct {
      int instruction_ptr;
      int token;
      std::string instruction;
      std::string value;
  } decoraded_nodes;
  
  int main () {
      std::list&lt;decoraded_nodes&gt; instruction_list;
      std::list&lt;decoraded_nodes&gt;::iterator it;
      it = instruction_list.begin();
  
      decoraded_nodes a;
      a.instruction_ptr = 1;
      a.token = 1;
      a.instruction = "1";
      a.value = "2";
  
      decoraded_nodes b;
      b.instruction_ptr = 1;
      b.token = 1;
      b.instruction = "1";
      b.value = "1";
  
      decoraded_nodes c;
      c.instruction_ptr = 1;
      c.token = 1;
      c.instruction = "1";
      c.value = "3";
  
      instruction_list.insert(it, a);
      instruction_list.insert(it, b);
      instruction_list.insert(it, c);
  
      bool aa = false;
      int bb = 0;
  
      for (std::list&lt;decoraded_nodes&gt;::iterator cur_instruc = instruction_list.begin(), 
          end = instruction_list.end(); 
          cur_instruc != end; ++cur_instruc) {
          cout &lt;&lt; cur_instruc-&gt;value;
  
          bb++;
  
          if (bb == 1) {
              aa = true;
          }
  
          if (aa) {
              cur_instruc--;
              aa = false;
          }
  
      }
  
      return 0;
  }
  </code></pre>