Vector iterators incompatible (runtime error)
<p>I am getting a runtime error saying that vector iterators incompatible.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

using namespace std;


struct Interval
{
public:
    Interval(int s,int e):start(s),end(e) {}
    int start;
    int end;

};


void merge_intervals(vector&lt;Interval&gt;&amp; Interval1, vector&lt;Interval&gt;&amp; Interval2, vector&lt;Interval&gt;&amp; merged)
{
    int i = 0 ;
    cout&lt;&lt;"i"&lt;&lt;i&lt;&lt;endl;
    vector&lt;Interval&gt;::iterator it1 = Interval1.begin();

    vector&lt;Interval&gt;::iterator it2 = Interval2.begin() ;

    while(it1 != Interval1.end() &amp;&amp; it2 !=Interval1.end())
    {
        cout&lt;&lt;"i"&lt;&lt;i&lt;&lt;endl;
        i++;
        if(it1-&gt;start &lt;= it2-&gt;start)
        {

            if(it1-&gt;end &lt;= it2-&gt;end)
            {
                it1++;
                merged.push_back(*it1);
            }
            else
            {
                it1++;
                it2++;
                merged.push_back(Interval(it1-&gt;start,it2-&gt;end));
            }
        }
        else
        {
            if(it1-&gt;end &gt;= it2-&gt;end)
            {
                it2++;
                merged.push_back(*it2);
            }
            else
            {
                it1++;
                it2++;
                merged.push_back(Interval(it2-&gt;start,it1-&gt;end));
            }
        }

        while(it1 != Interval1.end())
        {
            merged.push_back(*it1);
            *it1++;
        }

        while(it2 != Interval2.end())
        {
            merged.push_back(*it2);
            *it2++;
        }
    }
}

void print_intervals(vector&lt;Interval&gt;&amp; merged)
{
    vector&lt;Interval&gt;::iterator  it = merged.begin();

    for(it = merged.begin();it != merged.end(); ++it)
    {
        cout&lt;&lt;"("&lt;&lt;it-&gt;start&lt;&lt;","&lt;&lt;it-&gt;end&lt;&lt;")"&lt;&lt;endl;
    }
}

void test1()
{
    vector&lt;Interval&gt; Interval1, Interval2, merged ;

    Interval1.push_back(Interval(1,4));
    Interval1.push_back(Interval(6,10));
    Interval1.push_back(Interval(14,19));

    Interval2.push_back(Interval(13,17));

    merge_intervals(Interval1, Interval2, merged);
    print_intervals(merged);

}

int main()
{
    test1();
    return 0;
}
</code></pre>
