title: terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr
body: <p>I received this error: <em>"terminate called after throwing an instance of 'std::out_of_range'
    what():  basic_string::substr"</em> from this code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;cstdlib&gt;
  
  using namespace std;
  
  vector &lt;string&gt; n_cartelle;
  
  ifstream in("n_cartelle.txt");
  string linea;
  
  while(getline(in,linea))
  n_cartelle.push_back(linea);
  
  
  for(int i=0; i &lt; 4; ++i){
  
  
  if(n_cartelle[i].substr(n_cartelle[i].size()-3) == "txt")
  cout &lt;&lt;"si"&lt;&lt;endl;
  else
  cout&lt;&lt;"no"&lt;&lt;endl;
  
  }
  </code></pre>
  
  <p>If I try with this:</p>
  
  <pre><code>if(n_cartelle[7].substr(n_cartelle[7].size()-3) == "txt")
  cout &lt;&lt;"si "&lt;&lt;n_cartelle[7]&lt;&lt;endl;
  else
  cout&lt;&lt;"no"&lt;&lt;endl;
  </code></pre>
  
  <p>I don't get the error.</p>