关于std::lexicographical_compare的问题之第五个参数(函数指针)以及map/multimap自定义排序准则

using namespace std;

class Runtime 
{
public:
	enum type{nocase,normal};
private:
	type mode;
public:
	Runtime(type m) :mode(m) {};
	Runtime() :mode(normal) {};
	static bool com(char c, char d);
	bool operator ()(const string&x, const string&y);
	
};
bool Runtime::operator ()(const string&x, const string&y)
{
	if (mode == nocase) return x < y;
	else return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(),com);
}
bool Runtime::com(char c, char d)
{
	return toupper(c) < toupper(d);
}
typedef multimap<string,string,Runtime > Inset;
void fill(Inset &str)
{
	str.insert(make_pair("Sbs", "adabed"));
	str.insert(make_pair("Ccs", "czcdas"));
	str.insert(make_pair("SEs", "ghidn"));
	str.insert(make_pair("SDS", "ginvn"));
	str.insert(make_pair("CAs", "gocmad"));
	str.insert(make_pair("crs", "gocmad"));
	str.insert(make_pair("ads", "adcxcmad"));
	str.insert(make_pair("bds", "asdzzad"));
}
void print(Inset &x)
{
	Inset::iterator pos;
	pos = x.begin();
	while (pos != x.end())
	{
		cout << pos->first << ' ' << pos->second << endl;
		pos++;
	}
}

int main()
{
	Inset str;
	fill(str);
	print(str);
	cout << endl;
	Runtime st(Runtime::nocase);
	Inset st1(st);//multimap o(op);
	fill(st1);
	print(st1);
	//multimap<string, string>::iterator str1;

	
	system("pause");
	return 0;
}

ads adcxcmad
bds asdzzad
CAs gocmad
Ccs czcdas
crs gocmad
Sbs adabed
SDS ginvn
SEs ghidn


CAs gocmad
Ccs czcdas
SDS ginvn
SEs ghidn
Sbs adabed
ads adcxcmad
bds asdzzad
crs gocmad
请按任意键继续. . .

lexicographical_compare 最后要求的是一个普通函数指针,而不是成员函数指针,所以要加static

static 可以使该函数全局化,在栈中或者堆中创建,即不用声明对象全局下能够被调用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值