如何在c ++中按升序对结构数组进行排序

hof*_*ung 0 c++ arrays sorting struct

我有一个c ++数组.

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

radius LOCAL[1000];
Run Code Online (Sandbox Code Playgroud)

我想LOCAL根据值r按升序对数组进行排序.我在网上搜索过,大多数人建议使用sort().但许多人使用vector或不使用struct.(我不想用vector.)

Yu *_*Hao 5

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

std::sort(std::begin(LOCAL), std::end(LOCAL), [](const radius &a, const radius &b){return a.r < b.r});
Run Code Online (Sandbox Code Playgroud)