• <small id='nY97i'></small><noframes id='nY97i'>

    <i id='nY97i'><tr id='nY97i'><dt id='nY97i'><q id='nY97i'><span id='nY97i'><b id='nY97i'><form id='nY97i'><ins id='nY97i'></ins><ul id='nY97i'></ul><sub id='nY97i'></sub></form><legend id='nY97i'></legend><bdo id='nY97i'><pre id='nY97i'><center id='nY97i'></center></pre></bdo></b><th id='nY97i'></th></span></q></dt></tr></i><div id='nY97i'><tfoot id='nY97i'></tfoot><dl id='nY97i'><fieldset id='nY97i'></fieldset></dl></div>
    • <bdo id='nY97i'></bdo><ul id='nY97i'></ul>
      <legend id='nY97i'><style id='nY97i'><dir id='nY97i'><q id='nY97i'></q></dir></style></legend>
      1. <tfoot id='nY97i'></tfoot>
      2. 如何在 C++ 中使用自定义类型作为映射的键?

        How can I use a custom type as key for a map in C++?(如何在 C++ 中使用自定义类型作为映射的键?)

          • <legend id='GYe2l'><style id='GYe2l'><dir id='GYe2l'><q id='GYe2l'></q></dir></style></legend>
            <i id='GYe2l'><tr id='GYe2l'><dt id='GYe2l'><q id='GYe2l'><span id='GYe2l'><b id='GYe2l'><form id='GYe2l'><ins id='GYe2l'></ins><ul id='GYe2l'></ul><sub id='GYe2l'></sub></form><legend id='GYe2l'></legend><bdo id='GYe2l'><pre id='GYe2l'><center id='GYe2l'></center></pre></bdo></b><th id='GYe2l'></th></span></q></dt></tr></i><div id='GYe2l'><tfoot id='GYe2l'></tfoot><dl id='GYe2l'><fieldset id='GYe2l'></fieldset></dl></div>
              <bdo id='GYe2l'></bdo><ul id='GYe2l'></ul>
                  <tbody id='GYe2l'></tbody>

                <small id='GYe2l'></small><noframes id='GYe2l'>

                  <tfoot id='GYe2l'></tfoot>

                  本文介绍了如何在 C++ 中使用自定义类型作为映射的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将自定义类型指定为 std::map 的键.这是我用作键的类型:

                  I am trying to assign a custom type as a key for std::map. Here is the type which I am using as key:

                  struct Foo
                  {
                      Foo(std::string s) : foo_value(s){}
                  
                      bool operator<(const Foo& foo1) {   return foo_value < foo1.foo_value;  }
                  
                      bool operator>(const Foo& foo1) {   return foo_value > foo1.foo_value;  }
                      
                      std::string foo_value;
                  };
                  

                  std::map 一起使用时,出现以下错误:

                  When used with std::map, I am getting the following error:

                  error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Foo' (or there is no acceptable conversion) c:program filesmicrosoft visual studio 8vcincludefunctional 143
                  

                  如果我将 struct 更改为下面的那个,一切正常:

                  If I change the struct to the one below, everything works:

                  struct Foo
                  {
                      Foo(std::string s) : foo_value(s)   {}
                  
                      friend bool operator<(const Foo& foo,const Foo& foo1) { return foo.foo_value < foo1.foo_value;  }
                  
                      friend bool operator>(const Foo& foo,const Foo& foo1) { return foo.foo_value > foo1.foo_value;  }
                      
                      std::string foo_value;
                  };
                  

                  没有改变,只是操作符被重载为friend.为什么我的第一个代码不起作用?

                  Nothing changed, except that the operator is overloaded as friend. Why does my first code not work?

                  推荐答案

                  我怀疑你需要

                  bool operator<(const Foo& foo1) const;
                  

                  注意参数后面的const,这是为了使你的"(比较中的左侧)对象保持不变.

                  Note the const after the arguments, this is to make "your" (the left-hand side in the comparison) object constant.

                  只需要一个运算符的原因是它足以实现所需的排序.回答抽象的问题a 必须在 b 之前出现吗?"知道a是否小于b就足够了.

                  The reason only a single operator is needed is that it is enough to implement the required ordering. To answer the abstract question "does a have to come before b?" it is enough to know whether a is less than b.

                  这篇关于如何在 C++ 中使用自定义类型作为映射的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                  Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)
                • <i id='EWMi2'><tr id='EWMi2'><dt id='EWMi2'><q id='EWMi2'><span id='EWMi2'><b id='EWMi2'><form id='EWMi2'><ins id='EWMi2'></ins><ul id='EWMi2'></ul><sub id='EWMi2'></sub></form><legend id='EWMi2'></legend><bdo id='EWMi2'><pre id='EWMi2'><center id='EWMi2'></center></pre></bdo></b><th id='EWMi2'></th></span></q></dt></tr></i><div id='EWMi2'><tfoot id='EWMi2'></tfoot><dl id='EWMi2'><fieldset id='EWMi2'></fieldset></dl></div>
                    <bdo id='EWMi2'></bdo><ul id='EWMi2'></ul>
                        <tbody id='EWMi2'></tbody>
                          <legend id='EWMi2'><style id='EWMi2'><dir id='EWMi2'><q id='EWMi2'></q></dir></style></legend>

                          <small id='EWMi2'></small><noframes id='EWMi2'>

                          <tfoot id='EWMi2'></tfoot>