我要实现一个template function ,编译通过,运行的时候出现“Debug Assertion Failed” 错误???

时间:2008-05-12 13:50:06   来源:论坛整理  作者:  编辑:chinaitzhe
rt

代码:

C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include <algorithm>//泛型算法

#include <iostream>

#include <vector>

#include <functional>//函数模板



using namespace std;



template <typename InputIterator,typename ElemType,typename Comp,typename Input>

Input

sub_vec(InputIterator first,InputIterator last,Input at, const ElemType &val,Comp pred)

{

    sort(first,last);



    first=find_if(first,last,bind2nd(pred,val));



    at.erase(first,last);



    return at;

}



int main()

{

    int ia[6]={12,2,4,32,7,1};

    vector<int>ivec(ia,ia 6);



    ivec=sub_vec(ivec.begin(),ivec.end(),ivec,6,greater<int>());



    for(int ix=0;ix<ivec.size();ix  )

    cout << ivec[ix] << " ";

    cout << endl;



    return 0;

}






网友回复:上调试器,定位到是哪一行出错的再来。
网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





template <typename InputIterator,typename ElemType,typename Comp,typename Input>

Input

sub_vec(InputIterator first,InputIterator last,Input at, const ElemType &val,Comp pred)

{

    sort(first,last);

    if (last != at.end() && first != at.begin())

        cout<<"first and last have changed!\n";



    //first=find_if(first,last,bind2nd(pred,val));

    first=find_if(at.begin(),at.end(),bind2nd(pred,val));



    //at.erase(first,last);

    at.erase(first,at.end());



    return at;

}




网友回复:#include <algorithm>//泛型算法
#include <iostream>
#include <vector>
#include <functional>//函数模板

using namespace std;

template <typename InputIterator,typename ElemType,typename Comp,typename Input>
Input sub_vec(InputIterator first,InputIterator last,Input& at, const ElemType &val,Comp pred)//将参数Input改为引用
{
sort(first,last);

first=find_if(first,last,bind2nd(pred,val));

at.erase(first,last);

return at;
}

int main()
{
int ia[6]={12,2,4,32,7,1};
vector <int> ivec(ia,ia 6);

sub_vec(ivec.begin(),ivec.end(),ivec,6,greater <int>());//此处不需返回值

for(int ix=0;ix <ivec.size();ix )
cout < < ivec[ix] < < " ";
cout < < endl;

return 0;
}

网友回复:我先去上课了,=下再来看!
网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





template <typename ElemType,typename Comp,typename Input>

void sub_vec(Input& at, const ElemType &val,Comp pred)

{

    sort(at.begin(),at.end());

    typename Input::iterator first=find_if(at.begin(),at.end(),bind2nd(pred,val));

    at.erase(first,at.end());

}

int main()

{

    int ia[6]={12,2,4,32,7,1};

    vector<int>ivec(ia,ia 6);

    

    sub_vec(ivec,6,greater<int>());



    for(int ix=0;ix<ivec.size();ix  )

    cout << ivec[ix] << " ";

    cout << endl;



    return 0;

}


网友回复:谢谢大家的解答!!!我已经弄清楚了!!!
关键字:实现,一个,template,function,编译,

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面