title: c++ terminate called after throwing an instance 'std::out_of_range' what(): basic_string::substr: __pos > this->size()
body: <p>Problem:</p>
  <blockquote>
  <p>Given a word quiz with m blanks, clues and a maximum of n words per clues (3&lt;=m,n&lt;=100)</p>
  <p>The first line of the input contains m,n.</p>
  <p>The next m line contains the word quiz and the clues you need to fill.</p>
  <p>The blank which you need to fill is marked as 1 and the one you don't need to fill is marked as 0.</p>
  <p>The output is the word quiz after we fill it.</p>
  </blockquote>
  <p>So I try to solve as below, but it keeps being terminated. I am a beginner and this is my first time posting a problem, so any help will help me a lot!</p>
  <p><em><strong>Input:</strong></em></p>
  <pre class="lang-none prettyprint-override"><code>3 8
  011G1100
  1A111000
  00000C11
  CAT
  NIGHT
  DOG
  </code></pre>
  <p><em><strong>Output:</strong></em></p>
  <pre class="lang-none prettyprint-override"><code>0NIGHT00
  CANDY000
  00000CAT
  </code></pre>
  <p><em><strong>Code</strong></em></p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  int main(){
  
      short m,n;
  
      cin &gt;&gt; m &gt;&gt; n; //input
  
      vector &lt;string&gt; map(m); //map is the word quiz
  
      vector &lt;string&gt; clue(m); //clue
  
      for (int i=1; i&lt;=m;i++){
          string dd;
          cin &gt;&gt; dd;
          map.push_back(dd); //lines of the word quiz
      }
      cout &lt;&lt; endl;
  
      for (int i=1; i&lt;=m;i++){
          string dd;
          cin &gt;&gt; dd;
          clue.push_back(dd); //lines of clues
      }
  
      for (int i=1; i&lt;=m;i++){
          string str= map[i];
          int pos = str.find_first_not_of(&quot;0&quot;);      
          int pos2 = str.find_last_not_of(&quot;0&quot;);
          string str3 = str.substr (pos,pos2-pos+1);    //find the substring which don't have '0' 
  
          for (int j =1; j&lt;=m;j++){
              string mclue = clue [j];    
              if (str3.length() == mclue.length()){    //check if the length of the clue is the same as the length of the line
  
                  for(int k =0;k&lt;= str3.length();k++){
                      if (str3.at(k)==mclue.at(k)){    //check if there is a similar letter between the clue and the line
                          string str4 = str.replace(pos,pos2-pos+1,mclue);
                          cout &lt;&lt; str4; //output
                          break;
                      }
                  }
              }
          }
      }
  </code></pre>