<legend id='ruUO3'><style id='ruUO3'><dir id='ruUO3'><q id='ruUO3'></q></dir></style></legend>

    1. <small id='ruUO3'></small><noframes id='ruUO3'>

    2. <tfoot id='ruUO3'></tfoot>

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

    3. std:map 中的浮点键

      Floating point keys in std:map(std:map 中的浮点键)

        <small id='5IJeX'></small><noframes id='5IJeX'>

          • <bdo id='5IJeX'></bdo><ul id='5IJeX'></ul>
              <tfoot id='5IJeX'></tfoot>
              <legend id='5IJeX'><style id='5IJeX'><dir id='5IJeX'><q id='5IJeX'></q></dir></style></legend>
              <i id='5IJeX'><tr id='5IJeX'><dt id='5IJeX'><q id='5IJeX'><span id='5IJeX'><b id='5IJeX'><form id='5IJeX'><ins id='5IJeX'></ins><ul id='5IJeX'></ul><sub id='5IJeX'></sub></form><legend id='5IJeX'></legend><bdo id='5IJeX'><pre id='5IJeX'><center id='5IJeX'></center></pre></bdo></b><th id='5IJeX'></th></span></q></dt></tr></i><div id='5IJeX'><tfoot id='5IJeX'></tfoot><dl id='5IJeX'><fieldset id='5IJeX'></fieldset></dl></div>
                <tbody id='5IJeX'></tbody>
              • 本文介绍了std:map 中的浮点键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                下面的代码应该在存在的 std::map 中找到键 3.0.但由于浮点精度,它不会被找到.

                The following code is supposed to find the key 3.0in a std::map which exists. But due to floating point precision it won't be found.

                map<double, double> mymap;
                mymap[3.0] = 1.0;
                
                double t = 0.0;
                for(int i = 0; i < 31; i++)
                {
                  t += 0.1;
                  bool contains = (mymap.count(t) > 0);
                }
                

                在上面的例子中,contains 总是false.我目前的解决方法是将 t 乘以 0.1 而不是添加 0.1,如下所示:

                In the above example, contains will always be false. My current workaround is just multiply t by 0.1 instead of adding 0.1, like this:

                for(int i = 0; i < 31; i++)
                {
                  t = 0.1 * i;
                  bool contains = (mymap.count(t) > 0);
                }
                

                现在的问题:

                如果我使用 double 键,有没有办法向 std::map 引入模糊比较?浮点数比较的常见解决方案通常类似于 a-b <;epsilon.但是我没有看到使用 std::map 来做到这一点的直接方法.我真的必须将 double 类型封装在一个类中并覆盖 operator<(...) 来实现这个功能吗?

                Is there a way to introduce a fuzzyCompare to the std::map if I use double keys? The common solution for floating point number comparison is usually something like a-b < epsilon. But I don't see a straightforward way to do this with std::map. Do I really have to encapsulate the double type in a class and overwrite operator<(...) to implement this functionality?

                推荐答案

                您可以实现自己的比较功能.

                You could implement own compare function.

                #include <functional>
                
                class own_double_less : public std::binary_function<double,double,bool>
                {
                public:
                  own_double_less( double arg_ = 1e-7 ) : epsilon(arg_) {}
                  bool operator()( const double &left, const double &right  ) const
                  {
                    // you can choose other way to make decision
                    // (The original version is: return left < right;) 
                    return (abs(left - right) > epsilon) && (left < right);
                  }
                  double epsilon;
                };
                // your map:
                map<double,double,own_double_less> mymap;
                

                更新:参见有效 STL 中的第 40 项!根据建议更新.

                Updated: see Item 40 in Effective STL! Updated based on suggestions.

                这篇关于std:map 中的浮点键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                Adding types to the std namespace(将类型添加到 std 命名空间)
                Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                <i id='GXs5O'><tr id='GXs5O'><dt id='GXs5O'><q id='GXs5O'><span id='GXs5O'><b id='GXs5O'><form id='GXs5O'><ins id='GXs5O'></ins><ul id='GXs5O'></ul><sub id='GXs5O'></sub></form><legend id='GXs5O'></legend><bdo id='GXs5O'><pre id='GXs5O'><center id='GXs5O'></center></pre></bdo></b><th id='GXs5O'></th></span></q></dt></tr></i><div id='GXs5O'><tfoot id='GXs5O'></tfoot><dl id='GXs5O'><fieldset id='GXs5O'></fieldset></dl></div>
                  <bdo id='GXs5O'></bdo><ul id='GXs5O'></ul>

                        <tbody id='GXs5O'></tbody>

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

                        <tfoot id='GXs5O'></tfoot>
                      1. <legend id='GXs5O'><style id='GXs5O'><dir id='GXs5O'><q id='GXs5O'></q></dir></style></legend>