在你的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 whatoperators need to be implemented for std::map.find() to work. Forexample, I have a class MyString that wraps std::string, and which alsoimplements ==, <, <=, >, >=, etc. (Those operators are tested andworking correctly.)If I assign map["hello"] = "world", it saves the MyString''s correctlyin the map. But a subsequent call to map.find("hello") returnsmap.end().In addition to what the other poster said, I suspect that you forgotto 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 asubsequent map["hello"] = "something else" results in TWO items formap["hello"]... How is that possible? I thought that was only abehavior of multimap, but I suspect it''s related to my MyStringimplementation 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 secondassignment will destroy the first one, remember that the map hascopies.--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 whatoperators need to be implemented for std::map.find() to work. Forexample, I have a class MyString that wraps std::string, and which alsoimplements ==, <, <=, >, >=, etc. (Those operators are tested andworking correctly.)If I assign map["hello"] = "world", it saves the MyString''s correctlyin the map. But a subsequent call to map.find("hello") returnsmap.end(). Even more bizarre, a cout << on destruction suggests that asubsequent map["hello"] = "something else" results in TWO items formap["hello"]... How is that possible? I thought that was only abehavior of multimap, but I suspect it''s related to my MyStringimplementation not handling comparisons properly.Changing the map to < string, string > then works correctly, finds theitem, etc.So the question is, what does <string> implement that <MyString> needsin order to work as a Key in a std::map? Thanks in advance for yourhelp. 您应该提供完整的(简短的)测试计划,否则 社区没有机会 帮助你。从您的描述看来,可能有很多原因导致错误。一个简单的列表 的可能性: 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 (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 thecommunity has no chance tohelp you. From your description it seems, there could be many reasons oferror. A a simple listof possibilities:1) You have specialized std::less for your MyString class in a wayviolating strictly weak orderingrequirements.Note: This functor is used in std::map, if you don''t provide aspecial comparator. The standardimplementation of std::less<MyString> operator() will use operator<.2) At least your implementation of operator< of your MyString class doesnot work correctly, althoughyou claim it should work. This cannot be argued without itsimplementation, but you can easily test it:Just ensure that it obeys the strictly weakly comparablerequirements, 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) == falseand (y < x) == false3) 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>比较运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-16 00:32