- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
给定std::set< T, less >
或 std::map< T, less >
独特元素的容器。 less
是异构比较器。 IE。它可以比较另一种类型的值 U
针对 T
类型的值.而 T
类型的所有值是独一无二的,(也许)有很多类型的值 T
, 比较等于 U
类型的某个特定值.这是未定义的行为吗?
说,我想在容器中找到(一个)元素,它有键,相当于 U
类型的值.任何一个:第一个,最后一个或中间的一个(如果有的话)。我知道,容器中有不止一个元素,相当于值 u
类型 U
.我可以使用 std::set::find
吗?或 std::map::find
功能?是未定义的行为吗?
示例(此处与公差 0.2
进行了不精确的比较):
#include <set>
#include <iostream>
double const eps = 0.2;
struct less
{
bool operator () (double l, double r) const { return l < r; }
using is_transparent = void;
bool operator () (int l, double r) const { return l + eps < r; }
bool operator () (double l, int r) const { return l + eps < r; }
};
int main()
{
std::set< double, less > s{0.0, 0.9, 1.0, 1.1, 2.0};
for (auto it = s.find(1); it != std::end(s); it = s.find(1)) {
std::cout << *it << ' ';
s.erase(it);
}
}
输出(通常未指定顺序):
0.9 1 1.1
像上面那样使用唯一元素的关联有序容器是否是 UB?
我应该使用 std::multiset
吗?和 std::multimap
相反?
最佳答案
关联容器要求表前的说明文字说:
kl
is a value such thata
[sic] is partitioned ([alg.sorting]) with respect toc(r, kl)
, withr
the key value ofe
ande
ina
;ku
is a value such thata
is partitioned with respect to!c(ku, r)
;ke
is a value such thata
is partitioned with respect toc(r, ke)
and!c(ke, r)
, withc(r, ke)
implying!c(ke, r)
.
然后描述了a_tran.{find,count,equal_range}(ke)
, a_tran.lower_bound(kl)
和a_tran.upper_bound( ku)
。因此,要求是:
find
、count
和 equal_range
:
c(r, ke)
和 !c(ke, r)
c(r, ke)
必须暗示 !c(ke, r)
lower_bound
,容器中的元素必须根据 c(r, kl)
进行分区。upper_bound
,容器中的元素必须根据 !c(ku, r)
进行分区。只要您满足这些要求,使用异构查找与容器中的多个键等效的东西就没有错。 the original proposal 中的激励示例毕竟,它是关于在集合
名称中查找姓氏为“Smith”的每个人。
关于c++ - 映射或设置具有透明比较器和异构意义上的非唯一元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40502357/
我有一张 table People (First_Name, Last_Name)。此表包含与示例中一样重复的记录(并非所有行都重复): First_Name Last_Name John
我用 Java 编写过很多程序,之前也涉足过 C++。我在各种 C++ 书籍中阅读了有关指针的内容,并完成了书籍中的各种示例。 我了解指针的基础知识,但有一件事我一直不清楚。指针在现实世界中的应用是什
线 .Mappings(m => m.FluentMappings.AddFromAssemblyOf() 它有什么作用?它会在派生自 ClassMap 的 Product 类的程序集中查找任
我有用于打印数字的自定义打印功能。我制作了一个 ASCII 版本和一个 UTF-16LE 版本。 UTF-16LE 版本对 0-9 使用全角代码/字符,对十六进制使用 A-F。在调试我的函数时,我注意
这是我的代码片段: float ab(float); 以后 if(ab(temp)
我在一个项目文件中包含以下代码: //begin of the file ((window) => { 'use strict'; class View extends GSM.Event
我一直在到处寻找关于 ? 用法的正确解释。和 *。我注意到我可以使用以下方法拒绝所有用户的访问: 如果我想允许某个组,我应该在其上方添加下一行: 但是当我看到人们使用 ? 时,我开始忘记什么意思,
我正在关注 melon js tutorial .这是在我的 HUD.js 文件的顶部。 game.HUD = game.HUD || {} 我以前在其他例子中见过这个。 namespace.some
我正在处理一个包含数千行代码的文件。我正在第 700 行实现一个算法。我经常不得不离开这些行来检查文件中的其他方法。 导航回到我实际编码的地方通常很痛苦。如果我可以在第 700 行设置一个航路点并为其
我遇到了这段代码 do { if (higherQuality && w > targetWidth) { w /= 2; if (w &
uint8_t * const LCDMem = (uint8_t *) &LCDM3; 此代码在 msp430fg4618 培训套件中用于 lcd 配置。谁能解释一下上述代码的含义? 它允许使用 a
上下文 阅读一些内核代码。 问题 我不明白这行是什么意思 *(void **) &(int[2]){0,PAGE_SIZE}; 还有更多,这是什么意思 {0,PAGE_SIZE} 对我来说,它看起来不
我正在查看 Underscore.js 的源代码库,专门用于 map方法(该页面第 85 行左右,并复制到此处): _.map = function(obj, iterator, context)
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我是一名优秀的程序员,十分优秀!