C/C++--set排序

// TestVS2012.cpp : 定义控制台应用程序的入口点。
//

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

struct Test
{
	std::string name;
	int age;
};

class CustomSort
{
public:
	bool operator()(const Test &t1, const Test &t2) const
	{
		return t1.age < t2.age;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	std::set<Test, CustomSort> s;
	Test t1;
	t1.age = 2;
	t1.name = "a";

	Test t2;
	t2.age = 1;
	t2.name = "b";

	s.insert(t1);
	s.insert(t2);

	getchar();
	return 0;
}

你可能感兴趣的:(C++,排序,set,结构体,operator)