merge(iterator,iterator,iterator2,iterator2,outIterator)应该包含5个迭代器,分割发送的向量,然后对2个单独的向量进行排序,然后将它们合并为一个排序的向量。我的方法似乎有效,除非range2的元素小于range1,否则该元素将无法通过我的测试。
template<typename Iter1, typename Iter2, typename OIter>
OIter
merge (Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, OIter out)
{
// TODO
auto i = first1;
auto j = first2;
while(j != last2 && i != last1)
{
if(first2 == last2)
{
return std::copy(first1, last1, out);
}
if (*i < *j)
{
*out++ = *i++;
}else{
*out++ = *j++;
}
}
//only one of the ranges has elements copy them over
if(first2 == last2)
{
return std::copy(first1, last1, out);
}else{
return std::copy(first2, last2, out);
}
}
TEST:
需要(out == copy_out)
带有扩展名:
{0,1,2,3,0,1,2,3,0,0,0,0,0,0} =实际
==
{0,1,2,3,8,9,10,11,12,13,14,15,16,17,17 =预期
//only one of the ranges has elements copy them over
if(first2 == last2)
{
return std::copy(first1, last1, out);
}else{
return std::copy(first2, last2, out);
}
应该是
//only one of the ranges has elements left, copy them over if(j == last2) { return std::copy(i, last1, out); }else{ return std::copy(j, last2, out); }
还有这种特殊待遇
if(first2 == last2) { return std::copy(first1, last1, out); }
是不必要的(并且放错了位置。)>