- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试解决我的代码中的一个错误,似乎我无法真正理解标准中对 std::map::operator[] 的要求,或者至少在 STL 中存在一些问题我的编译器附带的(VS2013,仍在尝试不同的工具集)。
我可能误用的 std::map 被定义为提供比较类型作为其第三个模板参数,这使得我使用的一些关键对象与其他一些对象等效。至于我对标准 (23.2.4.2) 中给出的等价定义的理解:a 和 b 是等价的,如果:!(a<b)
和 !(b<a)
.
我面临的问题是,根据插入顺序,有时它还会插入具有等效项的键。
这是我最终得到的最小示例:
#include <map>
#include <cassert>
struct Foo {
int x;
char c;
};
struct FooLess {
bool equal_c( const Foo& f1, const Foo& f2) const {
return f1.c == f2.c;
}
bool operator() (const Foo& f1, const Foo& f2) const {
return !equal_c(f1, f2) && f1.x < f2.x;
}
};
using namespace std;
int main(int , char* [])
{
FooLess lt;
assert( !lt( Foo {1, 'a'}, Foo{3, 'a'}) );
assert( !lt( Foo {3, 'a'}, Foo{1, 'a'}) );
map<Foo, string, FooLess> m;
m[Foo{ 2, 'b'}] = "Foo(b)";
m[Foo{ 3, 'a'}] = "Foo(A)";
m[Foo{ 4, 'c'}] = "Foo(c)";
// does not hold!
assert ((m[Foo{1, 'a'}] == "Foo(A)") );
m[Foo{ 1, 'a'}] = "Foo(a)";
// does not hold!
assert(m.size() == 3);
return 0;
}
至于我对标准的解读,断言应该成立。
23.4.4.3 map element access [map.access]T& operator[](const key_type& x);
1 Effects: If there is no key equivalent to x in the map, inserts value_type(x, T()) into the map.
2 Requires: key_type shall be CopyConstructible and mapped_type shall be DefaultConstructible.
3 Returns: A reference to the mapped_type corresponding to x in *this.
4 Complexity: logarithmic.
为什么我错了?我是吗?
最佳答案
问题是 FooLess
没有描述严格的弱排序,而是偏序。
参见 C++ named requirements: Compare了解详情。
举个例子:
Foo a {1, 'a'};
Foo b {2, 'b'};
Foo c {1, 'c'};
FooLess comp;
assert(comp(a, b) && comp(b, c)); // Should imply comp(a, c)
assert(!comp(a, c)); // But doesn't due to only partial ordering
只要映射中或搜索到的所有键都具有唯一的 .c
,您的比较器就可以工作。
关于c++ - std::map operator[] 的一致性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56197572/
GhostScript PDF/A 生成好像有错误 当您通过 GhostScript 生成 PDF/A 文档时,当您单击 Adobe Reader 时,会出现一个一致性选项卡,其中显示: “一致性
我有一个需要测试的 XPath 引擎实现。 是否有一组标准的一致性测试可以用来验证是否符合 XPath 规范(与 XSLT 相关)。 什么将是完美的 XML 文档 XPath 表达式和预期的结果。 最
帮助我了解在这种情况下我可以期望与 MongoDB 的一致性级别。 我们正在运行一个副本集,其中 Mongoid 中的 consistency 标志设置为 strong,这意味着只读到 master。
假设我有一个采用一个参数的方法。 此参数应满足以下要求: 'of type':方法需要知道参数属于特定类(或子类)。 'implements interface':方法需要知道参数实现了特定的接口(i
当协议(protocol)将属性声明为可选而具体类型将其声明为非可选时,如何使具体类型符合协议(protocol)? 这是问题所在: protocol Track { var trackNum
我正在考虑使用浏览器的 navigator.mimeTypes 数组作为第三级用户/浏览器标识符。例如,当我在 Chrome 上运行时... console.log(navigator.mimeTyp
我有以下协议(protocol): protocol ProtoAInput { func funcA() } protocol ProtoA { var input: ProtoAI
如果选择“最终”一致性,则发生写入的区域内的一致性是什么? 如果我只需要区域强一致性,应该选择哪个选项? 最佳答案 如果您需要在主要区域内进行强读取,则应该选择强一致性或有界过时一致性。 关于azur
您好,我是一名初学者,目前正在尝试学习 java 编程。课本上的问题: 编写一个程序来帮助人们决定是否购买混合动力汽车。你的程序的输入应该是:•新车的成本•预计每年行驶里程•预计汽油价格 •每加仑英里
我正在尝试制作一个可以在 UILabel 上使用的 Swift 协议(protocol), UITextField , 和 UITextView包含他们的text , attributedText ,
我有一个类扩展: extension UICollectionViewCell { class func registerFromNibInCollectionView(collectionV
为了在 Swift 中模拟对象进行测试,我通常遵循这样的模式:编写一个协议(protocol)来描述我想要的对象的行为,然后使用 Cuckoo 为其生成模拟以进行测试。 通常,这些协议(protoco
假设我有两个非通用协议(protocol)(1) protocol StringValue { var asString: String {get} } protocol StringProv
我有一组协议(protocol)可以在 UITableView 中显示一个元素: protocol TableRepresentableRow { var title: String { get
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
用“class”标记 CacheManager 解决了我的问题。 案例:一个简单的缓存器,mutating get 不是我想要的,那么对于引用类型或类类型应该怎么做? protocol Cacher
我想要一个符合协议(protocol)的变量,但是 swift 编译器告诉我协议(protocol)没有确认。 protocol A {} protocol B { var a : A { g
如果我有一个类 Christmas 和一个协议(protocol) Merry,要使 Christmas 符合 Merry,很多人会这样做: class Christmas { ... } e
@objc public protocol P1 { func p1foo() } @objc public protocol P2 { func p2foo() } class A: NSO
我有一些结构符合的基本协议(protocol)(模型)。它们也符合 Hashable protocol Model {} struct Contact: Model, Hashable { v
我是一名优秀的程序员,十分优秀!