#include <iostream>
#include<set>
#include <string>
using namespace std;
class CMyDate {
protected:
int year, month, day;
string memo;
public:
CMyDate(){
}
CMyDate(int y, int m, int d, string s)
{
year = y;
month = m;
day = d;
memo = s;
}
void Show( )
{
printf("[%d-%d-%d] ", this->year, this->month, this->day);
cout << memo<<endl;
}
bool operator<(const CMyDate& y) const//重载运算符,很重要
{
return (year < y.year
|| year == y.year && month < y.month
|| year == y.year && month == y.month && day < y.day);
}
};
int main() {
set<CMyDate> cd;
CMyDate cm;
cd.insert(CMyDate(2018, 1, 1, "新年到来"));
cd.insert(CMyDate(2018, 12, 31, "一年结束"));
cd.insert(CMyDate(2019, 1, 1, "又是一年到来"));//添加信息
set<CMyDate>::iterator iter;
for (iter = cd.begin(); iter != cd.end(); iter++) {
cm = *iter;
cm.Show();//显示
}
}
C++使用set集合存放类的对象
最新推荐文章于 2022-10-08 00:00:00 发布