title: C++ string splitting segmentation error
body: <p>I am splitting a string into a vector of strings</p>
  
  <pre><code>    vector&lt;string&gt; tokens;
  
      stringstream strstm(str);
      string item;
      while (getline(strstm, item, ' ')) {
          tokens.push_back(item);
      }
  
      token_idx = 0;
  
      cout &lt;&lt; "size = " &lt;&lt; tokens.size() &lt;&lt; endl;
  
      for (unsigned int i = 0; i &lt; tokens.size(); i++)
      {
          cout &lt;&lt; tokens[i] &lt;&lt; "[" &lt;&lt; i &lt;&lt; "]" &lt;&lt; endl;
      } 
  </code></pre>
  
  <p>The split is successful, and the size() and its elements is what I like it to be. However the last token seems to act strangely when I try to get its value.</p>
  
  <pre><code>string Lexer::consume() {
      if (hasValue()) {
          token_idx++;
          cout &lt;&lt; "consumed " &lt;&lt; tokens[token_idx-1] &lt;&lt; " tokens = " &lt;&lt; token_idx -1 &lt;&lt; endl;
          return tokens[token_idx-1];
      }
      cout &lt;&lt; "didn't consume, token_idx = " &lt;&lt; token_idx &lt;&lt; endl;
      return "null";
  }
  </code></pre>
  
  <p>hasVal is like this</p>
  
  <pre><code>bool Lexer::hasValue() {
      if ( token_idx &lt; tokens.size()) {
          return true;
      } else {
          return false;
      }
  }
  </code></pre>
  
  <p>if i have an input string like such <code>1 + 2 * 3</code> the expected output from my program should be <code>(+1(*23))</code>, however I am getting a segmentation error.</p>
  
  <pre><code>size = 5
  1[0]
  +[1]
  2[2]
  *[3]
  3[4]
  consumed 1 tokens = 0
  consumed + tokens = 1
  consumed 2 tokens = 2
  consumed * tokens = 3
  consumed 3 tokens = 4
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>But if i change the has value check to <code>( token_idx &lt; tokens.size() -1 )</code>, the program will return <code>(+1 (*2 null))</code></p>
  
  <pre><code>size = 5
  1[0]
  +[1]
  2[2]
  *[3]
  3[4]
  consumed 1 tokens = 0
  consumed + tokens = 1
  consumed 2 tokens = 2
  consumed * tokens = 3
  didn't consume, token_idx = 4
  (+1 (*2 null))
  </code></pre>
  
  <p>So I'm wondering if there's a end of line after the 3 when splitting the way that I did or is there some other factors contributing to this behaviour? I am quite certain I am not going out of bounds for the vector though.</p>