[英]C++ Class & .H Sorting
Good evening experts and gurus !! 晚上好专家和大师!
I have an array object that record the following.. 我有一个数组对象记录以下..
This is at record.h 这是在record.h
Class Record
{
private:
string name;
int data;
float valueData;
public:
bool operator<(const Record&) const;
}
and the constructor are created at record.cpp 并在record.cpp创建构造函数
record.cpp i added this record.cpp我添加了这个
bool Record::operator<(const Record& rhs) const
{
return valueData < rhs.valueData;
}
At main.cpp I created record Array of Size 10 在main.cpp我创建了大小为10的记录数组
#include "record.h"
Record rec[10];
I did 我做到了
sort(&rec[0], &rec[2]);
but nothing seems changing or sorted .. i got 3 element , rec[0], rec[1], rec[2] and i want sort them ,but they are of another header file record.h & its record.cpp which describe above. 但似乎没有任何变化或排序 ..我有3个元素,rec [0],rec [1],rec [2]我想要排序它们,但它们是另一个头文件record.h及其record.cpp描述以上。
Original Question Next is i recorded several value to the object 原始问题接下来是我为对象记录了几个值
and now rec got 3 index 现在rec得到了3个索引
rec[0]
name = "jack1"
data = 1
valueData = 20
rec[1]
name = "jack2"
data = 2
valueData = 15
rec[2]
name = "jack3"
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 我想要实现的是做一个可以通过valueData最高升序形式重新排列这个数组的排序所以..它将是
rec[2]
then rec[0]
then rec[1]
.. rec[2]
然后rec[0]
然后rec[1]
..
but i wanna sort by class array object. 但我想按类数组对象排序。 and re-arrange the value together , how do i achieve this. 并重新安排价值,我如何实现这一目标。
the 3 value is private, so i not sure where do i create the sort function, at main.cpp or at record.cpp , next is how do i sort it so it can output in the way below.. 3值是私有的,所以我不知道我在哪里创建sort函数,在main.cpp或record.cpp,接下来是我如何对它进行排序,以便它可以以下面的方式输出..
-- Highest to lowest --
1) Name: Jack3, Data = 3, Value =25
2) Name: Jack1 , Data =1 , Value = 20
3) Name: Jack2, Data = 2, Value = 15
Thanks for all help and guide!! 感谢所有的帮助和指导!!
Do this instead: 改为:
sort(&rec[0], &rec[3]);
You were only sorting the 1st two elements, because the 2nd iterator in a range defined by an iterator pair by convention always points one past the end of the range you want to operate on. 您只是对前两个元素进行排序,因为按惯例由迭代器对定义的范围中的第二个迭代器始终指向一个超出您想要操作的范围的末尾的迭代器。
Use std::sort
in conjunction with something that compares two Record
s. 将std::sort
与比较两个Record
的东西结合使用。 There are at least 3 ways to provide this compoarison. 至少有3种方法可以提供这种比较。
1: Implement an operator<
on Record
: 1:实现operator<
on Record
:
class Record
{
public:
bool operator<(const Record& rhs) const
{
return valueData < rhs.valurData;
}
};
...then: ...然后:
sort(&rec[0], &rec[10]);
2: Provide a functor : 2:提供一个仿函数 :
struct compare_records : public std::binary_function<bool, Record, Record>
{
bool operator()(const Record& lhs, const Record& rhs) const
{
return lhs.valueData < rhs.valueData;
}
};
...then: ...然后:
sort_if(&rec[0], &rec[10], compare_records());
3: (If you have a C++11 compiler) Use a lambda: 3 :(如果你有一个C ++ 11编译器)使用lambda:
sort_if(&rec[0], &rec[10], [](const Record& lhs, const Record& rhs) -> bool
{
return lhs.vaklueData < rhs.valueData;
});
EDIT: 编辑:
Here is a complete sample that shows how to use the first method: 这是一个完整的示例,展示了如何使用第一种方法:
#include <cstdlib>
#include <map>
#include <algorithm>
using namespace std;
class Record
{
public:
string name;
int data;
float valueData;
public:
bool operator<(const Record& rhs) const
{
return valueData < rhs.valueData;
}
};
int main()
{
Record rec[10];
rec[0].name = "jack1";
rec[0].data = 1;
rec[0].valueData = 20;
rec[1].name = "jack2";
rec[1].data = 2;
rec[1].valueData = 15;
rec[2].name = "jack3";
rec[2].data = 3;
rec[2].valueData = 25;
sort(&rec[0], &rec[3]);
bool bk = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.