title: Simple 'for' loop crashing program on final iteration
body: <p>The below program seems to be crashing at the very end each time, and I'm assuming that this is because once I get to i = (size-1) then wordList[i+1] won't return anything, returns null, or something equivalent. Any way around this? Am I correct that this is the source of my problem?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;iomanip&gt;
  
  using std::cin;
  using std::cout;            using std::endl;
  using std::sort;
  using std::string;          using std::vector;
  
  // Obtain a list of words and return the de-duped list
  // with an accompanying word count
  int main()
  {
      cout &lt;&lt; "Enter a series of words separated by spaces, "
              "followed by end-of-file: ";
  
      vector&lt;string&gt; wordList;
      string x;
      while (cin &gt;&gt; x)
            wordList.push_back(x);
  
      typedef vector&lt;string&gt;::size_type vec_sz;
      vec_sz size = wordList.size();
      if (size == 0) {
         cout &lt;&lt; endl &lt;&lt; "This list appears empty.  "
                         "Please try again."  &lt;&lt; endl;
         return 1;
      }
  
      sort(wordList.begin(), wordList.end());
  
      cout &lt;&lt; "Your word count is as follows:" &lt;&lt; endl;
      int wordCount = 1;
      for (int i = 0; i != size; i++) {
          if (wordList[i] == wordList[i+1]) {
             wordCount++;
             }
          else {
               cout &lt;&lt; wordList[i] &lt;&lt; "    " &lt;&lt; wordCount &lt;&lt; endl;
               wordCount = 1;
               }
           }
      return 0;
  }
  </code></pre>