本人菜鸟近来学习c++,看书时看到一个如下的函数,想将其改成泛型算法但在删除的地方产生了疑惑vector的erase的删除是如何实现的而我仅仅依靠泛型指针能否实现删除操作呢?
//vector<int> sub_vec(const vector<int> &vec ,const int &value)
//{
// vector<int> local_vec(vec);
// sort(local_vec.begin(),local_vec.end());
// vector<int>::iterator iter = find_if(local_vec.begin(),local_vec.end(),bind2nd(greater<int>(),value));
// local_vec.erase(iter,local_vec.end());
// return local_vec;
//}
修改后
template<typename inputIterator,typename outputIterator,typename elemType>
outputIterator sub_vec(inputIterator first,inputIterator end,outputIterator ofirst,const elemType &value)
{
/*if(first==NULL&&end==NULL&&ofirst==NULL)
return NULL;*/
outputIterator at = ofirst;
while(first!=end) //复制
{
*at++ = *first++;
}
outputIterator oend = at;
sort(ofirst,oend);
outputIterator iter = find_if(ofirst,oend,bind2nd(greater<int>(),value));
for(;oend != iter;--oend)
{
at = oend;
--at;
cout<<"delete value:"<<*at<<endl;
?????
}
return oend;
}