65,184
社区成员




class wkrs
{
public:
wkrs() : num(""), name(""), sex(""), age(0) {}
wkrs(string s1, string s2, string s3, int a) : num(s1), name(s2), sex(s3), age(a){}
private:
string num;
string name;
string sex;
int age;
friend ostream & operator << (ostream & os, const wkrs & wk);
friend istream & operator >> (istream & is, wkrs & wk);
friend bool operator < (const wkrs & wk, const string & s);
};
////////////////////////////////////////////////////////////
//我知道是不能以下这么重载的, 但是要如何才能实现同时能比较类里的两个成员呢???
//
bool operator < (const wkrs & wk, const string & s)
{
return wk.num < s;
}
bool operator < (const wkrs & wk, const string & s)
{
return wk.name < s;
}
#include <string>
using namespace std;
class wkrs
{
public:
wkrs() : num(""), name(""), sex(""), age(0) {}
wkrs(string s1, string s2, string s3, int a) : num(s1), name(s2), sex(s3), age(a){}
private:
string num;
string name;
string sex;
int age;
friend ostream & operator << (ostream & os, const wkrs & wk);
friend istream & operator >> (istream & is, wkrs & wk);
friend bool operator < (const wkrs & wk, const string & s);
};
//既然要比较两个变量,那就这么修改吧,用pair模板的大小比较方法
//先要比较num和s,如果大于就再比较后面的name和s字符串,参考pair模板的写法
bool operator < (const wkrs & wk, const string & s)
{
return ((strcmp(wk.num.c_str(), s.c_str() ) < 0) ||
!(strcmp(wk.num.c_str(), s.c_str() ) < 0) &&(strcmp(wk.name.c_str(), s.c_str() ) < 0));
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
class less
{
private:
enum type {num, name, age};
type codition;
public:
bool less::operator ()(const wkrs &wk, const std::string &s)
{
if(condition == num) return wk.num < s;
if(condition == name) return wk.name < s;
if(condition == age) return wk.age < atoi(s.c_str());
}
less(type temp = num) : condition(temp) {}
};
bool operator < (const wkrs & wk, const string & s)
{
return wk.num < s;
}
bool operator < (const wkrs & wk, const string & s)
{
return wk.name < s;
}
为啥不把这俩放在一起呢?