的std ::地图< MyString,MyString>比较运算符? [英] std::map< MyString, MyString > comparison operator?
问题描述
这可能是一个非常明显的问题,但我不清楚
运算符需要为std :: map.find()实现。对于
示例,我有一个包含std :: string的MyString类,它还有
implements ==,<,< =,>,> =等等。(那些操作员已经过测试,并且
工作正常。)
如果我指定地图[" hello"] =" world",它在地图中正确保存了MyString'
。但随后对map.find(hello)的调用将返回
map.end()。更奇怪的是,一个cout<<在破坏时建议随后的地图[" hello] =其他东西。结果有两个项目
map [" hello]] ......怎么可能?我认为这只是多图的一个
行为,但我怀疑这与我的MyString
实现没有正确处理比较有关。
>
将地图更改为< string,string>然后正常工作,找到
项目等。
所以问题是,< string>实现< MyString>需要
才能在std :: map中作为Key工作?在此先感谢您的帮助。
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
This is probably a very obvious question, but I''m not clear on what
operators need to be implemented for std::map.find() to work. For
example, I have a class MyString that wraps std::string, and which also
implements ==, <, <=, >, >=, etc. (Those operators are tested and
working correctly.)
If I assign map["hello"] = "world", it saves the MyString''s correctly
in the map. But a subsequent call to map.find("hello") returns
map.end(). Even more bizarre, a cout << on destruction suggests that a
subsequent map["hello"] = "something else" results in TWO items for
map["hello"]... How is that possible? I thought that was only a
behavior of multimap, but I suspect it''s related to my MyString
implementation not handling comparisons properly.
Changing the map to < string, string > then works correctly, finds the
item, etc.
So the question is, what does <string> implement that <MyString> needs
in order to work as a Key in a std::map? Thanks in advance for your
help.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
推荐答案
jstanforth写道:
....
jstanforth wrote:
....
所以问题是,< string>是什么实现< MyString>需要
才能在std :: map中作为Key工作?在此先感谢您的帮助。
So the question is, what does <string> implement that <MyString> needs
in order to work as a Key in a std::map? Thanks in advance for your
help.
bool运营商<(..)const
是你们所有人需要声明。
听起来你的比较运算符有问题,或者你是b $ b复制构造函数。
bool operator<( .. ) const
Is all you need to declare.
It sounds like you have a problem with your comparison operator or you
copy constructor.
jstanforth写道:
jstanforth wrote:
这可能是一个非常明显的问题,但我不清楚需要为std ::
运算符实现什么? map.find()工作。对于
示例,我有一个包含std :: string的MyString类,它还实现了==,<,< =,>,> =等等。(那些运算符是测试并且
正常工作。)
如果我指定map [" hello"] =" world",它会正确地将MyString保存在地图中。但随后对map.find(hello)的调用返回
map.end()。
除了另一张海报所说的内容之外,我怀疑你忘了>
在你的MyString类上定义一个拷贝构造函数和/或operator =。
std :: map复制你的对象,这就是你需要它们的原因,
这可以解释为什么map.find(?hello?)返回map.end()。
更奇怪的是,一个cout<<关于破坏表明
后续地图[hello] =其他东西导致
map [" hello]的两个项目......这怎么可能?我认为这只是multimap的行为,但我怀疑它与我的MyString
实现无法正确处理比较。
This is probably a very obvious question, but I''m not clear on what
operators need to be implemented for std::map.find() to work. For
example, I have a class MyString that wraps std::string, and which also
implements ==, <, <=, >, >=, etc. (Those operators are tested and
working correctly.)
If I assign map["hello"] = "world", it saves the MyString''s correctly
in the map. But a subsequent call to map.find("hello") returns
map.end().
In addition to what the other poster said, I suspect that you forgot
to define a copy constructor and/or operator= on your class MyString.
std::map makes copies of your objects, that?s why you need them,
That could explain why map.find(?hello?) returns map.end().
Even more bizarre, a cout << on destruction suggests that a
subsequent map["hello"] = "something else" results in TWO items for
map["hello"]... How is that possible? I thought that was only a
behavior of multimap, but I suspect it''s related to my MyString
implementation not handling comparisons properly.
析构函数被调用两次是正常的,因为第二个
赋值会破坏第一个,记住地图有
副本。
-
马克点范点Peteghem在q-mentum dot com
http://www.q-mentum.com - 更简单,更强大的单元测试
[见 http://www.gotw.ca/resources/clcm.htm for info about
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
It''s normal that the destructor is called twice, because the second
assignment will destroy the first one, remember that the map has
copies.
--
Mark dot Van dot Peteghem at q-mentum dot com
http://www.q-mentum.com -- easier and more powerful unit testing
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Hello" jstanforth"
jstanforth schrieb:
Hello "jstanforth"
jstanforth schrieb:
这是可能是一个非常明显的问题,但我不清楚std :: map.find()需要实现什么运算符才能工作。对于
示例,我有一个包含std :: string的MyString类,它还实现了==,<,< =,>,> =等等。(那些运算符是测试并且
正常工作。)
如果我指定map [" hello"] =" world",它会正确地将MyString保存在地图中。但随后对map.find(hello)的调用将返回
map.end()。更奇怪的是,一个cout<<关于破坏表明
后续地图[hello] =其他东西导致
map [" hello]的两个项目......这怎么可能?我认为这只是multimap的行为,但我怀疑它与我的MyString
实现无法正确处理比较。
将地图更改为< string,string>然后工作正常,找到
所以问题是,什么做< string>实现< MyString>需要
才能在std :: map中作为Key工作?在此先感谢您的帮助。
This is probably a very obvious question, but I''m not clear on what
operators need to be implemented for std::map.find() to work. For
example, I have a class MyString that wraps std::string, and which also
implements ==, <, <=, >, >=, etc. (Those operators are tested and
working correctly.)
If I assign map["hello"] = "world", it saves the MyString''s correctly
in the map. But a subsequent call to map.find("hello") returns
map.end(). Even more bizarre, a cout << on destruction suggests that a
subsequent map["hello"] = "something else" results in TWO items for
map["hello"]... How is that possible? I thought that was only a
behavior of multimap, but I suspect it''s related to my MyString
implementation not handling comparisons properly.
Changing the map to < string, string > then works correctly, finds the
item, etc.
So the question is, what does <string> implement that <MyString> needs
in order to work as a Key in a std::map? Thanks in advance for your
help.
您应该提供完整的(简短的)测试计划,否则
社区没有机会
帮助你。从您的描述看来,可能有很多原因导致
错误。一个简单的列表
的可能性:
1)你的MyString类中有一个专门的std :: less
违反严格的弱订购
要求。
注意:这个仿函数用于std :: map,如果你不提供
特殊比较器。 std :: less< MyString>的标准
实现operator()将使用运算符<。
2)至少你的运算符实现<您的MyString类确实没有正常工作,但是你声称它应该有效。如果没有它的b $ b实现,这是不容争辩的,但是你可以很容易地测试它:
只要确保它服从严格弱的可比的
要求,它们是:
(a)(x
(b)如果x< y,而不是!(y< x)
(c)如果x< y和y< z比x < z
(d)等价:如果x和y相等,则(x
和(y
3)你的测试程序会导致不确定的行为或测试错误的东西。
来自不来梅的问候,
Daniel Krügler
[见 http://www.gotw .ca / resources / clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
You should provide a complete (short) test program, otherwise the
community has no chance to
help you. From your description it seems, there could be many reasons of
error. A a simple list
of possibilities:
1) You have specialized std::less for your MyString class in a way
violating strictly weak ordering
requirements.
Note: This functor is used in std::map, if you don''t provide a
special comparator. The standard
implementation of std::less<MyString> operator() will use operator<.
2) At least your implementation of operator< of your MyString class does
not work correctly, although
you claim it should work. This cannot be argued without its
implementation, but you can easily test it:
Just ensure that it obeys the strictly weakly comparable
requirements, which are:
(a) (x < x) == false
(b) if x < y, than !(y < x)
(c) if x < y and y < z than x < z
(d) Equivalence: If x and y are equivalent, than (x < y) == false
and (y < x) == false
3) Your test program causes undefined behaviour or tests the wrong thing.
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
这篇关于的std ::地图< MyString,MyString>比较运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!