结构体嵌套结构体,如何对嵌套的结构体进行调用快排?

发布网友

我来回答

1个回答

热心网友

试试我的代码:
# include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Re
{
struct F
{
double x;
double y;
};
};

int cmp(const struct Re::F &aa, const struct Re::F &bb) //::引用结构中的结构
{
if(aa.x < bb.x)
return 1;
return 0;
}

int main (void)
{
vector<Re::F> v;
struct Re::F f1, f2;
f1.x = 2.1;
f1.y = 2.1;
v.push_back(f1);

f2.x = 1.1;
f2.y = 1.1;
v.push_back(f2);

sort(v.begin(), v.end(), cmp);
for(vector<Re::F>::iterator it = v.begin(); it != v.end();it++)
{
cout << (*it).x << "\t" << (*it).y << endl;
}
return 0;}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com