程序片段----std::set中使用自己定义的结构体需要重载小于号 20170904

这篇博客介绍了在C++中,当在std::set中存储自定义结构体时,需要重载小于号运算符(operator<)以确保集合的正确排序。内容还涉及了DLL与静态库的区别,以及Excel和Matlab的数据交互函数。
摘要由CSDN通过智能技术生成


// 在 set 中使用自己的结构体

#include "stdafx.h"
#include <iostream>
#include <set>

struct student
{
	std::string name;
	int age;
	bool is_boy;
};

/// 需要自己写比较大小的规则
bool operator<(const student& lhs, const student& rhs) {
	char a = lhs.name.front();
	/// name 按照首字母的askii比较
	if(lhs.name.front() == rhs.name.front())
	{
		/// name 相同,比较 age
		if(lhs.age == rhs.age)
		{
			/// age 相同,比较 is_boy
			return lhs.is_boy < rhs.is_boy;
		}
		else
			return lhs.age < rhs.age;
	}
	else
		return lhs.name.front() < rhs.name.front();

}


int _tmain(int argc, _TCHAR* argv[])
{
	student A = {std::string("A"), 20, true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值