作为一个练习,我在模板中实现了快速排序算法,它对于元素数量较少(最多约 760 个)的向量“工作正常”,但为元素数量较多的向量提供了 seqfault。有人可以告诉我我做错了什么:
template< typename Vector, typename VecElem > void qsort(Vector *pv)
{
if (pv->size()<=1) return;
VecElem p;
Vector *pvl=new Vector,*pvr=new Vector;
p = pv->back();
pv->pop_back();
pvr->push_back(p);
for (auto it=pv->begin();it!=pv->end();it++)
{
if (*it < p) pvl->push_back(*it);
else pvr->push_back(*it);
}
qsort<Vector,VecElem>(pvl);
qsort<Vector,VecElem>(pvr);
if (pvl->size()) *pv = *pvl;
if (pvr->size()) std::copy(pvr->begin(), pvr->end(), std::back_inserter(*pv));
delete pvl;
delete pvr;
}