- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这个定义在 OuterClass
中:
struct Compare
{
bool operator ()(const T&, const T&);
};
typedef set<T, Compare> MySet;
我的问题是比较函数 operator ()
取决于 OuterClass
的状态。 (MySet
实例在优化算法期间使用,它们必须在不同阶段以不同方式排序。)
是否有任何方法/变通方法可以从比较函数 operator ()
中访问 OuterClass
的非静态成员?
最佳答案
Is there any way/workaround to access nonstatic members of OuterClass from within the compare function operator ()?
有。只需为 Compare
编写一个用户定义的构造函数,它接受并存储对 OuterClass
的引用,这样:
struct Compare
{
Compare(OuterClass& o) : oc(o) { }
bool operator ()(const T&, const T&)
{
// Uses oc somehow...
}
private:
OuterClass& oc;
};
然后,当您创建集合时,您可以执行以下操作:
int main()
{
typedef std::set<T, Compare> MySet;
OuterClass oc; // <== Construct an object of type Outerclass somehow...
MySet ms(Compare(oc)); // <== Construct your comparator and pass it
// in input to the constructor of std::set
}
但请注意:排序标准应保持稳定。对于同一集合,元素必须始终比较相同。根据 C++11 标准的第 23.2.4/3 段:
The phrase “equivalence of keys” means the equivalence relation imposed by the comparison and not the
operator==
on keys. That is, two keysk1
andk2
are considered to be equivalent if for the comparison objectcomp
,comp(k1, k2) == false && comp(k2, k1) == false
. For any two keys k1 and k2 in the same container, callingcomp(k1, k2)
shall always return the same value.
关于C++ STL 集 : Compare object with extrinsic state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16261441/
我经常有一个 Comparator 类型,而我需要一个 Comparable 类型,反之亦然。是否有可重用的 JDK API 可以相互转换?类似的东西: public static Comp
我怎么能写这个 Comparator sort = (i1, i2) -> Boolean.compare(i2.isOpen(), i1.isOpen()); 像这样(代码不起作用): Compa
请帮助她。我有一个错误 Collections.sort(var4, new Comparator() { public int compare(TreeMap var1, TreeMa
学习 Kotlin,我试图了解 Java 的 Comparator接口(interface)有效 - 主要是 compare() 函数,这样我就可以利用它。 我已经尝试阅读 compare() 的文档
我有以下程序 List numbers = Arrays.asList("10", "68", "97", "9", "21", "12"); Collections.sort(numbers, (
我想根据嵌套类的属性对如下所示的列表进行排序。 class Test { private NestedClass nestedClass; private AnotherNes
我很好奇“Beyond Compare”的算法是如何工作的? 我猜想他们使用了一种标准的(众所周知的?)算法来实现“字符与字符”的比较。你知道这个算法的名字吗?谢谢 最佳答案 Beyond Compa
这个问题已经有答案了: How does the sort() method of the Collection class call the Comparable's compareTo()? (1
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicates: difference between compare() and compareTo() Java: What i
我被要求为某个类实现Comparable或Compartor,我们称之为V。 假设我有一个 V 的 Collection 或 Set(还不确定,但我认为这并不重要)。 V 有一个方法,可以评估它的“权
我正在查看Java8中实现的Comparator.comparing方法的源代码 这是代码 public static Comparator comparing( Function
假设我有一个类 ClassA,它的属性是 ClassB: public ClassA { private String attr; private ClassB classB; } p
我有一个自定义比较器,其比较逻辑如下: List l = new ArrayList(); l.add("tendercoupon"); l.add("giftcard
我正在努力实现一个处理 Comparator 和 Comparable 接口(interface)的层次结构。我不清楚的几件事: 如果我将比较器添加到比较器链中,这段代码究竟意味着什么 chain.a
正在关注 this question关于按另一个列表对列表进行排序,我尝试做同样的事情 - 但由于某种原因它对我不起作用。我错过了什么? List nums = Arrays.asList(5
假设我有一个像这样的领域模型: class Lecture { Course course; ... // getters } class Course { Teache
在表达式 > 中像这样的签名 public static > foo(T x) { ... } T的描述递归地依赖于Comparable . 如果T延伸Comparable ,和Comparable延
所有“数字”比较器(例如 Comparer.Default 、 Comparer.Default 等)返回 -1 的原因是什么? , 0或 1 ,但是 Comparer.Default和 Compar
(如果这是重复的,请指出正确的答案!我搜索并阅读了几个(> 5)个相关问题,但似乎没有一个是正确的。还查看了泛型常见问题解答和其他来源...) 当一个集合类接受一个比较器时,它应该具有 Compara
SBCL 1.3.1 综上所述,a是一个列表,'(7),b通过setq sbcl This is SBCL 1.3.1.debian, an implementation of ANSI Common
我是一名优秀的程序员,十分优秀!