按数据成员对对象的矢量进行排序
Sorting vectors of objects by data members
是的,我知道这是一个重复的问题,我已经知道我要找的anwser在这里:
根据对象的属性对对象向量进行排序
然而,我在将其转换为自己的代码时遇到了问题。我正在看上面问题的代码片段:
struct SortByX
{
bool operator() const(MyClass const& L, MyClass const& R) {
return L.x < R.x;
}
};
std::sort(vec.begin(), vec.end(), SortByX();
我不明白的是MyClass const & L
和MyClass const & R
所代表的是什么。我不知道如何将其应用到我的代码中。
为了提供更详细的信息,我将把3个排序方法放入对象向量的包装类中,这些对象向量的参数为(string
、double
、double
、double
、bool
)。总体目标是根据string
、bool
和3个双打中的任何一个对vector
进行排序。
这是我拥有的最新版本:
void StationVector::sortByGrade(int kindOfGas) {
struct SortByGrade {
int kindOfGas;
SortByGrade(int kindOfGas) :
kindOfGas(kindOfGas) {
}
bool operator()(GasStation const &L, GasStation const & R) const {
return L.getPrice(kindOfGas) < R.getPrice(kindOfGas);
}
};
std::sort(localStations.begin(), localStations.end(),
SortByGrade(kindOfGas));
}
SortByGrade(kindOfGas))
行给了我以下错误:
调用"sort(__gnu_cxx::__normal_iterator>>,__gnu_xx::__normal_iterator>>,model::StationVector::sortByGrade(int)::SortByGrad)"时没有匹配的函数
SortByX
是一个二元谓词函子。二进制谓词意味着它接受两个参数并返回一个布尔值。Functor意味着它的实例是可调用的。例如:
MyClass a = ....;
MyClass b = ....;
SortByX comp;
bool flag = comp(a,b); // call this SortByX instance with two MyClass instances
现在,std::sort
将在内部使用您传递的SortByX
实例的副本,以便在std::vector<MyClass>
的元素之间执行排序该向量所需的比较。
std::vector<MyClass> v;
// fill the vector with MyClass objects
std::sort(v.begin(), v.end(), SortByX()); // sort the vector using a SortByX instance for comparisons
注意:为了实现这一点,二进制谓词必须实现严格的弱排序。
我不明白的是MyClass const&L、 和MyClass常量&R.
在本例中,L
和R
是正在比较的容器中的两个项(类MyClass
的实例),L
在小于运算符的左侧,R
在右侧。它们是通过常量引用传入的。
我不知道如何将其应用到我的代码中。
在您自己的bool operator() const(MyClass const& L, MyClass const& R)
中,您需要比较您在问题中提到的三个数据成员,尤其要记住应用严格弱排序。如果L
"小于"R
,则返回true
,否则返回false
。
正在跟踪问题的更新。。。
看起来您希望将一个变量传递到函子中。您可以通过创建一个构造函数来实现这一点,比如SSCCE(在这里编译):
#include <algorithm>
#include <vector>
namespace model {
struct GasStation
{
double getprice(int kindOfGas) const
{
return kindOfGas;
}
};
struct StationVector
{
std::vector<GasStation> localStations;
struct SortByGrade
{
int kindOfGas_;
SortByGrade(int kindOfGas)
:kindOfGas_(kindOfGas)
{
}
bool operator() (GasStation const &L, GasStation const & R) const
{
// You'll need other comparisons here, but this is a good start...
return L.getprice(kindOfGas_) < R.getprice(kindOfGas_);
}
};
void sortByGrade(int kindOfGas)
{
std::sort(localStations.begin(), localStations.end(), SortByGrade(kindOfGas));
}
};
}
int main()
{
model::StationVector sv;
sv.sortByGrade(0);
}
注意:const
限定符位于参数列表之后,而不是方法名称之后。
另外,请不要把整个方法放在一行上,这会让它很难阅读。
- 用于访问容器<T>数据成员的正确 API
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 数据成员SFINAE的C++17测试:gcc vs clang
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 静态数据成员模板专用化的实例化点在哪里
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 使用指针访问数组中的对象数据成员
- 友元函数无法访问私有数据成员 (c++)
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 对多个数据成员进行排序
- 排序链表-移动节点还是交换数据成员
- 使用 #include 按<algorithm>字符串数据成员对类对象的向量进行排序
- 根据数据成员对对象数组进行排序或筛选
- 排序类对象数据成员从大到小
- 按特定数据成员对结构的 STL 容器进行排序
- 如何在另一个类数据成员上定义“std::set”排序
- 对<MyClass> MyClass 的一个数据成员进行排序向量?
- 如何在所示代码中对类的两个数据成员数组进行排序
- 按数据成员对对象的矢量进行排序