title: terminated by signal SIGSEGV (Address boundary error) when dealing with iterators
body: <p>I'm trying to implement merge sort algorithm. First I'm trying to create the merge method. I'm using vectors and utilizing iterators. But the <code>while</code> body in <code>merge</code> function causes this error:</p>
  
  <blockquote>
    <p>“./a.out” terminated by signal SIGSEGV (Address boundary error)</p>
  </blockquote>
  
  <p>Here's the code </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  std::vector&lt;int&gt; merge(std::vector&lt;int&gt; &amp;, std::vector&lt;int&gt; &amp;);
  
  int main()
  {
    std::vector&lt;int&gt; vect {1,3,5,7};
    std::vector&lt;int&gt; vect2 {2,3,6,8};
    std::vector&lt;int&gt; temp_vect = merge(vect, vect2);
    for(int num: temp_vect) {
      std::cout &lt;&lt; num  &lt;&lt; std::endl;
    }
  
  }
  
  std::vector&lt;int&gt; merge(std::vector&lt;int&gt; &amp; first_vect, std::vector&lt;int&gt; &amp; second_vect)
  {
    std::vector&lt;int&gt; sorted_vect;
    auto sorted_it = sorted_vect.begin();
    auto first_it = first_vect.begin(), second_it = second_vect.begin();
  
    while(first_it != first_vect.end() || second_it != second_vect.end()) {
        if(*first_it &lt; *second_it) {
          sorted_vect.push_back(*first_it);
          first_it++;
        } else if(*first_it &gt; *second_it) {
          sorted_vect.push_back(*second_it);
          second_it++;
        } else {
          sorted_vect.push_back(*first_it);
          sorted_vect.push_back(*second_it);
          first_it++; second_it++;
        }
    }
  
    if(first_it == first_vect.end()) {
      //end of first_vect reached
      //inserting the rest of second_vect
      sorted_vect.insert(sorted_vect.end() - 1, second_it, second_vect.end());
    } else if (second_it == second_vect.end()) {
      //end of second_vect reached
      //inserting the rest of first_vect
      sorted_vect.insert(sorted_vect.end() - 1, first_it, first_vect.end());
    }
  
    return sorted_vect;
  }
  </code></pre>