2
std::map<String, double> m1,m2;
m1["A"] = 20;
m2["A"] = 20.01;

if (m1 == m2)
    cout << "True";
else
    cout << "False";

示例代码打印 False,因为 20 不等于 20.1。但是在我的应用程序中,我想将这些值视为相等,因为这些值之间的差异在允许的公差范围内。那么有什么方法可以为数据(不是Key)提供自定义比较功能吗?

任何帮助表示赞赏。

编辑:对不起代码中的错误。我复制了我试图找到这个问题的解决方案的代码。对于我的方案,键必须相等。

4

4 回答 4

3

如果您只关心整个容器的平等,我会推荐该::std::equal算法。就是这样:

const double tolerance = 0.01;
bool equal = m1.size() == m2.size() &&
             ::std::equal(m1.begin(), m1.end(), m2.begin(),
                          [tolerance](const decltype(m1)::value_type &a,
                                      const decltype(m2)::value_type &b) {
    return (a.first == b.first) &&
           (::std::abs(a.second - b.second) < tolerance);
});

如果您关心“小于”的关系,那么::std::lexicographical_compare这就是您想要的。这需要 C++11 lambda 功能才能工作。

如果您真正想要的是以模糊方式比较相等的数据值,我会向您介绍一些技巧(并且还需要一些 C++11 特性)fuzzy-double.cpp。我禁用排序比较,因为这会诱使您将这些东西填充到排序容器中,并且由于(2.0 == 2.1) && (2.1 == 2.2), 但是(2.0 != 2.2),它们不适合此目的。

#include <cmath>
#include <iostream>

template <const double &tolerance>
class fuzzy_double {
 public:
   fuzzy_double(double x) : x_(x) {
      static_assert(tolerance >= 0, "tolerance must be >= 0");
   }

   operator double() const { return x_; }
   const fuzzy_double &operator =(double x) { x_ = x; }

   bool equals(const fuzzy_double &b) const {
      return ::std::abs(x_ - b.x_) <= tolerance;
   }
   // This cannot be used as the basis of a 'less than' comparison operator for
   // the purposes of other algorithms because it's possible for a transitive
   // equality relationship to exit that equates all fuzzy_double's to
   // eachother. There is no strict ordering that makes sense.
   bool fuzzy_less(const fuzzy_double &b) const {
      return (b.x_ - x_) > tolerance;
   }

 private:
   double x_;
};

template <const double &tolerance>
bool operator ==(const fuzzy_double<tolerance> &a,
                 const fuzzy_double<tolerance> &b)
{
   return a.equals(b);
}

template <const double &tolerance>
bool operator !=(const fuzzy_double<tolerance> &a,
                 const fuzzy_double<tolerance> &b)
{
   return !a.equals(b);
}

template <const double &tolerance>
bool operator <(const fuzzy_double<tolerance> &a,
                const fuzzy_double<tolerance> &b)
{
   // tolerance < 0 should be an impossible condition and always be false, but
   // it's dependent on the template parameter and so only evaluated when the
   // template is instantiated.
   static_assert(tolerance < 0, "fuzzy_doubles cannot be ordered.");
   return false;
}

template <const double &tolerance>
bool operator >=(const fuzzy_double<tolerance> &a,
                const fuzzy_double<tolerance> &b)
{
   // tolerance < 0 should be an impossible condition and always be false, but
   // it's dependent on the template parameter and so only evaluated when the
   // template is instantiated.
   static_assert(tolerance < 0, "fuzzy_doubles cannot be ordered.");
   return false;
}

template <const double &tolerance>
bool operator >(const fuzzy_double<tolerance> &a,
                const fuzzy_double<tolerance> &b)
{
   // tolerance < 0 should be an impossible condition and always be false, but
   // it's dependent on the template parameter and so only evaluated when the
   // template is instantiated.
   static_assert(tolerance < 0, "fuzzy_doubles cannot be ordered.");
   return false;
}

template <const double &tolerance>
bool operator <=(const fuzzy_double<tolerance> &a,
                 const fuzzy_double<tolerance> &b)
{
   // tolerance < 0 should be an impossible condition and always be false, but
   // it's dependent on the template parameter and so only evaluated when the
   // template is instantiated.
   static_assert(tolerance < 0, "fuzzy_doubles cannot be ordered.");
   return false;
}

extern constexpr double ten_e_minus_2 = 0.01;

int main()
{
   fuzzy_double<ten_e_minus_2> a(3);
   fuzzy_double<ten_e_minus_2> b(3.009);
   fuzzy_double<ten_e_minus_2> c(2.991);
   fuzzy_double<ten_e_minus_2> d(3.011);
   fuzzy_double<ten_e_minus_2> e(2.989);

   using ::std::cout;

   cout << "a == a: " << (a == a) << '\n';
   cout << "a == b: " << (a == b) << '\n';
   cout << "a == c: " << (a == c) << '\n';
   cout << "a == d: " << (a == d) << '\n';
   cout << "a == e: " << (a == e) << '\n';
   return 0;
}

C++11 不允许一个double,甚至const一个,作为模板参数。OTOH,它确实允许指向具有外部链接的对象的指针和引用作为模板参数。因此,如果您将公差声明为 anextern constexpr double您可以使用命名的公差作为模板参数。

于 2012-03-29T18:02:25.107 回答
1

您正在比较两个完全独立的地图,其中包含不同的键和值。我很确定这不是你的意图。

您可以使用键的自定义比较操作制作地图,以回答我认为您要问的问题,但是制作具有可变容差的地图可能效果不佳。STL 对小于和等于的含义以及它们的行为方式有严格的要求;如果您违反规则,您的地图将随机失败。所以我不会为此使用地图。在您的情况下,您对值感兴趣,而不是键,在这种情况下,STL 根本不关心(它根本不查看您的值)。

于 2012-03-29T18:01:01.937 回答
1

您可以使用lexicographical_compare忽略值中微小差异的自定义对比较器,如下所示:

bool mycomp (pair<string,double> lhs, pair<string,double> rhs) {
    if (lhs.first < rhs.first) {
        return true;
    }
    if (lhs.first > rhs.first) {
        return false;
    }
    // Here is the "check tolerance" part:
    if (abs(lhs.second-rhs.second) < 0.05) {
        return false;
    }
    return lhs.second < rhs.second;
}

int main() {
    std::map<string, double> m1,m2;
    m1["A"] = 20;
    m2["A"] = 20.01;
    bool res = lexicographical_compare(
        m1.begin(), m1.end(),
        m2.begin(), m2.end(),
        mycomp
    );
    cerr << res << endl;
    return 0;
}
于 2012-03-29T18:14:17.253 回答
0

当您比较 M1 和 M2 时,您是在比较地图,而不是地图中的值。如果您想比较双打,请查看这篇文章

于 2012-03-29T18:06:42.320 回答