sort函数c语言结构体 sort语句c语言( 七 )


/*按照降序排列*/
bool operator(const PAIR x, const PAIR y)
{
return x.point_valuey.point_value;
}
则会报错如下错误:
std::sort因为函数参数不明确,所以无法推导出模板参数等.
实验结果
本次实验是基于这样一个问题的:有一些坐标点集合(2d的坐标点 , 坐标点之间没有重复),每个坐标点对应一个数,现在需要对这些数排序从而达到对这些坐标点排序 。有尝试过把点的坐标和它对应的值放在map中,然后对map中的元素用std::sort()进行排序 , 但是由于开始没有发现那个重载符号的使用,所以没有调试成功 。现在直接不用map了,而是用vector,vector里面放的是带有坐标点和其对应值的struct 。
本次实验是在vector中存入3个结构体对象,每个结构体中放入一个二维点和它对应的值,然后采用sort()对齐排序 , 排序结果如下:
/*按照降序排列*/
bool operator(const PAIR x, const PAIR y)
{
return x.point_valuey.point_value;
}
///*按照降序排列*/
//bool compare(const PAIR x, const PAIR y)
//{
//return x.point_valuey.point_value;
//}
void main()
{
PAIR pair1, pair2, pair3;
std::vectorPAIR vec;
pair1.point = Point(10, 20);
pair1.point_value = https://www.04ip.com/post/100;
pair2.point = Point(70, 30);
pair2.point_value = https://www.04ip.com/post/99;
pair3.point = Point(44, 76);
pair3.point_value = https://www.04ip.com/post/101;
vec.push_back(pair1);
vec.push_back(pair2);
vec.push_back(pair3);
//std::sort(vec.begin(), vec.end(), compare);
std::sort(vec.begin(), vec.end());
cout"排序的结果为:"endl;
for(vectorPAIR::iterator it = vec.begin(); it != vec.end(); ++it) {
coutit-pointendl;
关于sort函数c语言结构体和sort语句c语言的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。