如何在c+中按升序对结构数组进行排序+; 我有一个C++数组。< /P> struct radius {double r; double angle; double x; double y;}; radius LOCAL[1000];

如何在c+中按升序对结构数组进行排序+; 我有一个C++数组。< /P> struct radius {double r; double angle; double x; double y;}; radius LOCAL[1000];,c++,arrays,sorting,struct,C++,Arrays,Sorting,Struct,我想根据值r按升序对LOCAL数组进行排序。 我在互联网上搜索过,大多数人建议使用sort()。 但许多人使用向量或非结构。(我不想使用vector)该函数可以满足您的需要。也可以在C中使用++ int-radius\u比较(常数void*a1,常数void*a2) { 半径*r1=(半径*)a1; 半径*r2=(半径*)a2; //TODO:实现正确的浮点比较 返回r1->rr-1: r1->r>r2->r?1: 0; } qsort(LOCAL,sizeof(LOCAL)/sizeof(LO

我想根据值r按升序对
LOCAL
数组进行排序。 我在互联网上搜索过,大多数人建议使用
sort()
。 但许多人使用
向量
或非
结构
。(我不想使用
vector

该函数可以满足您的需要。也可以在C中使用++

int-radius\u比较(常数void*a1,常数void*a2)
{
半径*r1=(半径*)a1;
半径*r2=(半径*)a2;
//TODO:实现正确的浮点比较
返回r1->rr-1:
r1->r>r2->r?1:
0;
}
qsort(LOCAL,sizeof(LOCAL)/sizeof(LOCAL[0]),sizeof(radius),radius\u compar);
重要的是,请查看有关浮点比较的一些详细信息

以下是如何使用std::sort执行此操作:

bool RadiusLessThan( const radius& r1, const radius& r2 )
{
    // Note that this is not a safe way to compare floats
    // it's just for illustration.
    return r1->r < r1->r;
}

std::sort( LOCAL, LOCAL+sizeof(LOCAL)/sizeof(LOCAL[0]), RadiusLessThan );
bool RadiusLessThan(常数半径&r1,常数半径&r2)
{
//请注意,这不是比较浮动的安全方法
//这只是为了举例说明。
返回r1->rr;
}
std::sort(LOCAL,LOCAL+sizeof(LOCAL)/sizeof(LOCAL[0]),RadiusLessThan);
您可以使用qsort()函数

#include <iostream>
#include <cstdlib>

const int LOCAL_SIZE = 10;

struct radius {
    double r; double angle; double x; double y;
};

int ascending(const void *d1, const void *d2);

int main(void) {
    radius LOCAL[LOCAL_SIZE] = {
        { -1, },
        { 0, },
        { 7, },
        { 2, },
        { 6, },
        { 5, },
        { 8, },
        { 9.9, },
        { 138, },
        { -1666 }
    };
    qsort(LOCAL, LOCAL_SIZE, sizeof(radius), ascending);

    for (radius &d : LOCAL) {
        std::cout << d.r << std::endl;
    }

    return 0;
}

int ascending(const void *pd1, const void *pd2) {
    const radius *r1 = (const radius *)pd1, *r2 = (const radius *)pd2;
    if (r1->r > r2->r)
    {
        return 1;
    }
    else if (r1->r < r2->r)
    {
        return -1;
    }
    else
    {
        return 0;
    }
}
#包括
#包括
const int LOCAL_SIZE=10;
结构半径{
双r;双角度;双x;双y;
};
整数递增(常数无效*d1,常数无效*d2);
内部主(空){
局部半径[局部大小]={
{ -1, },
{ 0, },
{ 7, },
{ 2, },
{ 6, },
{ 5, },
{ 8, },
{ 9.9, },
{ 138, },
{ -1666 }
};
qsort(本地、本地大小、大小(半径)、升序);
用于(半径和直径:本地){
标准::cout r2->r)
{
返回1;
}
否则如果(r1->rr)
{
返回-1;
}
其他的
{
返回0;
}
}

std::sort
需要两个迭代器,它也适用于本机数组。您只需告诉它如何比较两个
radius

std::sort(std::begin(LOCAL), std::end(LOCAL), [](const radius &a, const radius &b){return a.r < b.r});
std::sort(std::begin(LOCAL),std::end(LOCAL),[](const radius&a,const radius&b){返回a.r
除了已经提供的答案, 在结构定义中,使用sort()时可以重写“”,可以提供一个比较函数作为第三个参数,也可以为结构定义<(小于)运算符:

struct radius {
   double r; double angle; double x; double y;
   bool operator < (const radius& another) const {
       return r < another.r;
   }
};

通过定义小于运算符,您的结构也可以放入
集合

cygnus-software.com链接的文章说它已过时,并重定向到更新版本。您是否尝试使用
结构的非
向量
进行
排序
#include <iostream>
#include <cstdlib>

const int LOCAL_SIZE = 10;

struct radius {
    double r; double angle; double x; double y;
};

int ascending(const void *d1, const void *d2);

int main(void) {
    radius LOCAL[LOCAL_SIZE] = {
        { -1, },
        { 0, },
        { 7, },
        { 2, },
        { 6, },
        { 5, },
        { 8, },
        { 9.9, },
        { 138, },
        { -1666 }
    };
    qsort(LOCAL, LOCAL_SIZE, sizeof(radius), ascending);

    for (radius &d : LOCAL) {
        std::cout << d.r << std::endl;
    }

    return 0;
}

int ascending(const void *pd1, const void *pd2) {
    const radius *r1 = (const radius *)pd1, *r2 = (const radius *)pd2;
    if (r1->r > r2->r)
    {
        return 1;
    }
    else if (r1->r < r2->r)
    {
        return -1;
    }
    else
    {
        return 0;
    }
}
std::sort(std::begin(LOCAL), std::end(LOCAL), [](const radius &a, const radius &b){return a.r < b.r});
radius::operator<(const radius &y)
{
    if(this->r < y.r)
         return true;
    else
         return false;   
}
struct radius {
   double r; double angle; double x; double y;
   bool operator < (const radius& another) const {
       return r < another.r;
   }
};
sort(std::begin(LOCAL), std::end(LOCAL));