注册 登录
编程论坛 C++教室

请教一下sort函数的用法

i80286 发布于 2013-09-30 13:46, 590 次点击
sort函数如何使用自建头文件中的函数对数组进行排序?自写的头文件和子程序如下:
#ifndef TEMP_H_
#define TEMP_H_
class temp
{
private:
    int a,b;
    std::string str;
public:
    void test();
    bool Up (const temp &s1, const temp &s2);
};
#endif
子程序:
void temp::test()
{
    vector<temp> A;
    temp c;
    for(int i=0;i!=5;i++)
    {
        c.a=i;
        c.b=i+1;
        getline(cin,c.str);
        A.push_back(c);
    }
    sort(A.begin(),A.end(),Up);
    for(vector<temp>::iterator iter=A.begin();iter!=A.end();iter++)
    {
       cout<<(*iter).str<<"\t"<<(*iter).a<<"\t"<<(*iter).b<<endl;
    }
}
bool temp::Up (const temp &s1,const temp &s2)
{
    return s1.str<s2.str;
}
编译出错,VS2010提示:函数调用缺少参数列表;请使用“&temp::Up”创建指向成员的指针
3 回复
#2
rjsp2013-09-30 13:56
static bool Up (const temp &s1, const temp &s2);
#3
i802862013-09-30 14:00
回复 2楼 rjsp
问题解决,能说一下为什么要如此定义?谢谢
#4
blueskiner2013-09-30 20:42
查阅C++手册会发现,标准函数std::sort的声明,最后的参数必须为合法函数的入口。
1