注册 登录
编程论坛 VC++/MFC

c++中对于list 模板中的sort的引用 怎么就错了人-> i.sort(sortp);

亢奋青年 发布于 2014-03-04 09:14, 708 次点击
#include<iostream>
#include<list>
using namespace std;
bool sortp(const int& l,const int& k)
{
    //define criteria for list::sort :return ture for desired oder
    return (l>k);
}
template<typename T>
void display(const T& input)
{
    for(T::const_iterator jj=input.begin();jj!=input.end();jj++)
    {
        cout<<*jj;
    }
    cout<<endl;
}

int main()
{
    list<int> i;
    i.push_front(3);
    i.push_front(3);
    i.push_front(9);
    i.push_front(1);
    i.push_front(0);
    i.push_back(5);
   
    cout<<" the fist  list :";
    display(i);

    i.sort();

    cout<<"after sort:"<<endl;
    display(i);
    i.sort(sortp);
    cout<<" oder new:"<<endl;
    display(i);
    return 0;
}
 error C2664: 'void __thiscall std::list<int,class std::allocator<int> >::sort(struct std::greater<int>)' : cannot convert parameter 1 from 'bool (const int &,const int &)' to 'struct std::great
er<int>'
        No constructor could take the source type, or constructor overload resolution was ambiguous
执行 cl.exe 时出错.
4 回复
#2
yuccn2014-03-04 12:03
cannot convert parameter 1 from 'bool (const int &,const int &)' to 'struct std::great
er<int>'  

你看看sort 的申明?
#3
亢奋青年2014-03-04 21:20
回复 2楼 yuccn
对到书上打了啊  21天学会C
+
+
#4
亢奋青年2014-03-04 21:25
回复 2楼 yuccn
这不  是定义函数 sortP的  一个二元谓词吗?  帮助比较 sort函数判断  一个元素是否比另一个小啊!
#5
peach54602014-03-05 11:09
哦,那就是应该烧书了
1