C ++排序类数组 [英] C++ Sorting Class Array

查看:90
本文介绍了C ++排序类数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++排序数组类



我有一个数组对象,记录以下内容。



classone.h

  ClassOne 
{
string name;
int data;
float valueData;
}

,构造函数在 classone.cpp



在main.cpp我创建了SizeO的ClassOne数组

  #includeclassone.h

ClassOne cone [10];

接下来是我为对象记录了几个值



现在ClassOne有3个物件

  cone [0] 
name =hello
data = 1
valueData = 20

cone [1]
name =panda
data = 2
valueData = 15
b $ b cone [2]
name =joe
data = 3
valueData = 25

我想实现的是做一个排序,可以通过 valueData 最高上升形式重新排列这个数组,所以..



cone [2] 然后 cone [0] 1] ..



但问题,如果我使用气泡排序,我尝试google并找到一些,他们正在排序例如 int a [] = {9,6,5,23,2,6,2,7,1,8};



但我想通过类数组对象排序。



所以当我cout它将是



<$> p $ p> - 最高到最低 -
1)名称:Joe,Data = 3,Value = 25
2)名称:Hello,Data = 1,Value = 20
3)名称:Panda,Data = 2,Value = 15



<感谢您的帮助和指导!!

解决方案

最简单的方法是使用标准库:

  #include< algorithm> 

std :: sort(cone,cone + 10,
[](ClassOne const& a,ClassOne const& b) - > bool
{return a。 value< b.value;});

如果你愿意在全局定义一个比较操作符,你甚至不需要lambda:

  bool operator<(ClassOne const& a,ClassOne const& b)
{
return a.value<价值
}

std :: sort(cone,cone + 10);

或者你可以使比较器成为一个成员函数。或者你可以给比较函数一个自定义名称,并传递作为 sort 的第三个参数。在比较特定于您的情况而不是自然的情况下,这可能是个好主意:

  bool ValueCmp ClassOne const& a,ClassOne const& b)
{
return a.value<价值
}

std :: sort(cone,cone + 10,ValueCmp);

最后一个版本是有用的,如果你没有C + + 11支持在第一种情况下),或者如果您想在多种不同的情况下重用比较器。


C++ Sorting Array Class

I have an array object that record the following..

This is at classone.h

ClassOne
{
string name;
int data;
float valueData;
}

and the constructor are created at classone.cpp

At main.cpp I created ClassOne Array of Size 10

#include "classone.h"

ClassOne cone[10];

Next is i recorded several value to the object

and now ClassOne got 3 objects

cone[0]
name = "hello"
data = 1
valueData = 20

cone[1]
name = "panda"
data = 2
valueData = 15

cone[2]
name = "joe"
data = 3
valueData = 25

What i want to achieve is do a sort that can rearrange this array by valueData highest ascending form so.. it will be

cone[2] then cone[0] then cone[1] ..

but the issue if i use bubble sort , i tried google and find some, they are sorting by e.g int a[]={9,6,5,23,2,6,2,7,1,8};

but i wanna sort by class array object. and re-arrange the value together , how do i achieve this.

So when i cout it will be

-- Highest to lowest --
1) Name: Joe , Data = 3, Value =25
2) Name: Hello , Data =1 , Value = 20
3) Name: Panda, Data = 2, Value = 15

Thanks for all help and guide!!

解决方案

The easiest way is to use the standard library:

#include <algorithm>

std::sort(cone, cone + 10,
          [](ClassOne const & a, ClassOne const & b) -> bool
          { return a.value < b.value; } );

If you're willing to define a comparison operator globally, you don't even need the lambda:

bool operator<(ClassOne const & a, ClassOne const & b)
{
    return a.value < b.value;
}

std::sort(cone, cone + 10);

Or you could make the comparator a member function. Or you could give the comparator function a custom name and pass that as the third argument of sort. This might be a good idea in the case where the comparison is specific to your situation and not "natural":

bool ValueCmp(ClassOne const & a, ClassOne const & b)
{
    return a.value < b.value;
}

std::sort(cone, cone + 10, ValueCmp);

The last version is useful if you don't have C++11 support (for lambdas, as in the first case), or if you want to reuse the comparator in multiple different situations.

这篇关于C ++排序类数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆