title: How do I erase a part of a string using str.begin() without incurring errors?
body: <p>Given a string, I have to return the middle character or characters(if the string is even) of the string. This is what I came up with.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  std::string input = "test";
  std::string get_middle(std::string input) 
  {
  
  if (input.size() % 2 == 0) {
          input.erase(input.begin(), (input.size() / 2) - 1);
          input.erase(input.begin() + 2, (input.size() / 2) - 1);
      }
      else {
          input.erase(input.begin(), (input.size() - 1) / 2);
          input.erase(input.begin() + 1,(input.size() - 1) / 2);
      }
    return input;
  }  
  </code></pre>
  
  <p>The errors have always been at the input.begin() or input.erase() part. Curiously, this example I found on <a href="http://www.cplusplus.com/reference/string/string/erase/" rel="nofollow noreferrer">http://www.cplusplus.com/reference/string/string/erase/</a> works even when it looks the same as mine:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  int main ()
  {
    std::string str ("This is an example sentence.");
    std::cout &lt;&lt; str &lt;&lt; '\n';
                                             // "This is an example sentence."
    str.erase (10,8);                        //            ^^^^^^^^
    std::cout &lt;&lt; str &lt;&lt; '\n';
                                             // "This is an sentence."
    str.erase (str.begin()+9);               //           ^
    std::cout &lt;&lt; str &lt;&lt; '\n';
                                             // "This is a sentence."
    str.erase (str.begin()+5, str.end()-9);  //       ^^^^^
    std::cout &lt;&lt; str &lt;&lt; '\n';
                                             // "This sentence."
    return 0;
  }
  </code></pre>
  
  <p>What seems to be the issue here?</p>