set和struct类型结合怎么用count函数啊,报错了!!!
  • 板块学术版
  • 楼主haha_hua
  • 当前回复5
  • 已保存回复5
  • 发布时间2024/8/27 16:19
  • 上次更新2024/8/27 19:16:50
查看原帖
set和struct类型结合怎么用count函数啊,报错了!!!
754658
haha_hua楼主2024/8/27 16:19

set和struct类型结合怎么用count函数啊,报错了!!! 54行处那有问题,求指教!!!

#include<iostream>
#include<set>

using namespace std;

struct Person
{
	string name;
	float score;
	Person(){};
	Person(string name,float score)
	{
		this->name = name;
		this->score = score;	
	}
	
};

//class OR struct 都可以 
class cmp1
{
public:
	bool operator()(Person x1,Person x2)
	{
		return x1.score > x2.score;
	}
};

void print(set<Person,cmp1> &ob)
{
	for(set<Person,cmp1>::const_iterator i = ob.begin(); i != ob.end(); i++)
	{
		cout << (*i).name << " " << (*i).score << endl;
	}
}

int main()
{
	set<Person,cmp1> ob;
	
	ob.insert(Person("bob",100));
	ob.insert(Person("lucy",99));
	print(ob);
	if(ob.find(Person("bob",100)) != ob.end())
	{
		cout << "YES" << endl;
	} else
	{
		cout << "NO" << endl;
	}
	
	cout << ob.count(Person("bob",100)) << endl;
	return 0;
}
2024/8/27 16:19
加载中...