vs2022下set容器存储对象及重写容器排序方式的谓词时,报错C3848、C2664

struct myfunc 
{
	 bool operator()(int v1,int v2)const
	 **/*当使用set容器存储对象时,无法进行比较,此时重写谓词,函数的const缺少会报错 错误C3848	具有类型“const myfunc”的表达式会丢失一些 const - volatile 限定符以调用“bool myfunc::operator ()(const int, const int)”*/**
	{
		return v1 > v2;
	}
};
void test02() {
	set<int, myfunc> myset2;//此处调用
	myset2.insert(1);
	myset2.insert(100);
	myset2.insert(28);
	myset2.insert(43);
	myset2.insert(5);
	myset2.insert(20);
	myset2.insert(59);
	for_each(myset2.begin(), myset2.end(), myprint);

}
struct myfunc2 {
	bool operator()( const Maker& m1, const Maker& m2)const
	**/*这里参数的const不能少 如果少了会报错C2664	“bool myfunc2::operator ()(Maker &,Maker &) const”: 无法将参数 1 从“const _Kty”转换为“Maker &” 
	当使用set容器存储对象时,无法进行比较,此时重写谓词,函数的const缺少会报错 C3884会导致报错具有类型xxx的表达式会丢失一些xxxx限定符以调用xxxx*/**
	{
		return m1.age > m2.age;
	}
};
template <class T>
void printmyfunc(T& m) {
	for (set<Maker, myfunc2>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << it->name << "  " << it->age << endl;
	}
}
void test05() {
	set<Maker,myfunc2> mymaker;
	mymaker.insert(Maker("aaaa", 18));
	mymaker.insert(Maker("bbbb", 15));
	mymaker.insert(Maker("cccc", 17));
	printmyfunc(mymaker);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值