您当前的位置: 首页 >  c++
  • 0浏览

    0关注

    1477博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C++ set 排序 修改元素之后不会改变原来的排序

软件工程小施同学 发布时间:2021-10-31 11:21:33 ,浏览量:0

#include 
#include 
#include 
using namespace std;
 
/*Student结构体*/
struct Student {
	string	name;
	int	id;
	int	score;
};
 
/*“仿函数"。为Student set指定排序准则*/
class studentSortCriterion {
public:
	/*类型要与set容器类型一致*/
	bool operator()( const Student *a, const Student *b ) const
	{
		// 成绩相等,按照id从小到大排;成绩不相等,按照成绩从大到小排
		return( (a->score == b->score) ? (a->id < b->id) : (a->score > b->score) );
	}
};
 
 
int main()
{
	set	stuSet;
	set	stus;
 
	Student stu1, stu2, stu3;
	stu1.name	= "张三";
	stu1.id		= 1;
	stu1.score	= 100;
 
	stu2.name	= "李四";
	stu2.id		= 2;
	stu2.score	= 90;
 
	stu3.name	= "小明";
	stu3.id		= 3;
	stu3.score	= 100;
 
	stuSet.insert( &stu1 );
	stuSet.insert( &stu2 );
	stuSet.insert( &stu3 );

	
 
	/* 遍历 */
	for ( std::set::iterator it = stuSet.begin(); it != stuSet.end(); it++ )
	{
		std::cout name score             
关注
打赏
1665320866
查看更多评论
0.0426s