我有以下课程:
public class SupplierCategory : IEquatable<SupplierCategory>
{
public string Name { get; set; }
public string Parent { get; set; }
#region IEquatable<SupplierCategory> Members
public bool Equals(SupplierCategory other)
{
return this.Name == other.Name && this.Parent == other.Parent;
}
#endregion
}
public class CategoryPathComparer : IEqualityComparer<List<SupplierCategory>>
{
#region IEqualityComparer<List<SupplierCategory>> Members
public bool Equals(List<SupplierCategory> x, List<SupplierCategory> y)
{
return x.SequenceEqual(y);
}
public int GetHashCode(List<SupplierCategory> obj)
{
return obj.GetHashCode();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
我正在使用以下linq查询:
CategoryPathComparer comparer = new CategoryPathComparer();
List<List<SupplierCategory>> categoryPaths = (from i …Run Code Online (Sandbox Code Playgroud) 这是一个后续问题,询问 如何在不重载operator(),std :: less,std :: greater的情况下为std :: multiset提供自定义比较器?
并且我试图通过以下方式解决。
可以向类的成员提供自定义比较lambda函数(自c ++ 11以来),std::multiset如下所示:
#include <iostream>
#include <set>
const auto compare = [](int lhs, int rhs) noexcept { return lhs > rhs; };
struct Test
{
std::multiset<int, decltype(compare)> _set{compare};
Test() = default;
};
Run Code Online (Sandbox Code Playgroud)
很简单。
Test班上的成员是
std::map<std::string, std::multiset<int, /* custom compare */>> scripts{};
Run Code Online (Sandbox Code Playgroud)
我尝试使用std::multisetwith自定义
Compare(案例-1)std::greater<> (案例-2)前两个选项是成功的。但是,lambda作为自定义比较功能的情况却无法正常工作。这是MCVC:https ://godbolt.org/z/mSHi1p
#include <iostream>
#include <functional>
#include <string>
#include <map>
#include <set>
const …Run Code Online (Sandbox Code Playgroud) 我想按字母顺序对给定的 TreeView 子节点进行排序。
假设我的树视图是这样的:
第一个节点1
第一个节点2
我想对每个 firstNode 的 secondNodes 中的节点进行排序。
我该怎么做?- 我对自定义比较器有红色但不明白如何在我的情况下使用它。
假设我有一个像这样的简单类:
class Test {
public:
Test(int reference) { m_reference = reference; }
void feed(int x) { m_data.push_back(x); }
int get() { return m_data.front(); }
private:
int m_reference;
std::vector<int> m_data;
};
Run Code Online (Sandbox Code Playgroud)
std::vector我想用值代替,而不是std::priority_queue。而不是返回的.front()值,我想.get()该.top()值priority_queue基于自定义比较功能。假设此自定义比较是根据值和实例之间的绝对差来计算的reference。
我不知道如何std::priority_queue在类属性中声明。
我试过了:
bool compare(int a, int b) {
return std::abs(a - m_reference) < std::abs(b - m_reference);
}
Run Code Online (Sandbox Code Playgroud)
然后:
std::priority_queue<int, std::vector<int>, decltype(&Test::compare)> m_priority;
Run Code Online (Sandbox Code Playgroud)
我也尝试过std::function这样,但这会引发多个错误:
std::function<bool(int a, int b)>> pq([this](int a, int …Run Code Online (Sandbox Code Playgroud) 我想要一个用于以下代码的自定义比较器。但是,我不允许重载 operator(), std::less, std::greater。
我试图使用 lambda 来实现这一点,但gcc不允许我auto用作非静态成员。还有其他方法可以使这项工作吗?
#include <iostream>
#include <map>
#include <set>
class Test
{
public:
// bool operator () (const int lhs, const int rhs) { // not allowed
// return lhs > rhs;
// };
using list = std::multiset<int /*, Test*/>;
std::map<const char*, list> scripts;
};
int main()
{
Test t;
t.scripts["Linux"].insert(5);
t.scripts["Linux"].insert(8);
t.scripts["Linux"].insert(0);
for (auto a : t.scripts["Linux"]) {
std::cout << a << std::endl;
}
std::cout << "end";
} …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个用于比较字符串的自定义比较器的elses代码.
我注意到,如果至少有一个字符串参数为null,它将会失败.
比较根据比较结果返回-1,0或1.
如果这样的代码被修复以处理空值,如果是这样,如果其中一个参数为null,它应该返回什么?
这可能听起来像一个愚蠢的问题,但我想知道很长一段时间有一个更好的方法:
struct X
{
int a;
int b;
};
bool sortComp(const X first, const X second)
{
if (first.a!=second.a)
return (first.a<second.a);
else
return (first.b<second.b);
}
class setComp
{
public:
bool operator() (const X first, const X second) const
{
if (first.a!=second.a)
return (first.a<second.a);
else
return (first.b<second.b);
}
};
int main()
{
vector<X> v;
set<X, setComp> s;
sort(begin(v), end(v),sortComp);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我实现了两次相同的功能,一次用于排序,一次用于集合中的隐式排序.有没有办法避免代码重复?
这个简单的自定义比较器用于tuple<int, int, int>下面的示例测试的类型崩溃。我检查cout了cmp比较器中的语句,每次调用cmp都会得到一个返回值,所以它不像元组 t1 和 t2 中的值是垃圾。
知道为什么会崩溃吗?这个非常简单的自定义比较器有什么问题吗?
class Solution {
public:
vector<int> assignBikes(vector<vector<int>>& ws, vector<vector<int>>& bs) {
int n = ws.size(), m = bs.size();
vector<tuple<int, int, int>> dist;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int d = abs(ws[i][0]-bs[j][0]) + abs(ws[i][1]-bs[j][1]);
dist.push_back(make_tuple(d, i, j));
}
}
sort(dist.begin(), dist.end(), cmp());
}
struct cmp {
bool operator() (tuple<int, int, int>& …Run Code Online (Sandbox Code Playgroud) 我现在正在学习如何使用std :: find和自定义比较器.
但是,通过在线指导,我面临编译器错误.
链接到我的代码.
以下是我的代码:
#include <iostream>
#include <algorithm>
#include <pair.h>
#include <vector>
using namespace std;
int main()
{
struct comp
{
comp(const int& input) : _input(input) {}
bool operator()(const pair<int, int>& iPair)
{
return (iPair.first == _input);
}
int _input;
};
pair<int, int> pair1(1,3);
pair<int, int> pair2(2,4);
vector<pair<int, int> > vec;
vec.push_back(pair1);
vec.push_back(pair2);
vector<pair<int,int> >::iterator it = find(vec.begin(), vec.end(), comp(1));
if(it != vec.end())
{
cout << it->second << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误如下:
In function …Run Code Online (Sandbox Code Playgroud)