首页 > 代码库 > 自己创建map结构体 sort + cmp

自己创建map结构体 sort + cmp

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Point
{
	int x, y;
};

struct Map
{
	Point p;
	float f;
};

bool cmp(Map x, Map y)
{
	return x.f < y.f;
}

int main()
{
	vector<Map> vec;
	
	for (int i = 0; i < 10; i ++)
	{			
		Map mp;
		mp.p.x = i;
		mp.p.y = i+2;
		mp.f = (5-i) * 2.3;
		vec.push_back(mp);		
	}	
	
	for (int i = 0; i < vec.size(); i ++)
	{
		cout << vec[i].f << " ";
	}
	cout << "\n";
	
	sort(vec.begin(), vec.end(), cmp);
	
	for (int i = 0; i < vec.size(); i ++)
	{
		cout << vec[i].f << " ";
	}
	cout << "\n";
	
	return 0;
} 

自己创建map结构体 sort + cmp