Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7720869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:46:41+00:00 2026-06-01T03:46:41+00:00

std::map<String, double> m1,m2; m1[A] = 20; m2[A] = 20.01; if (m1 == m2) cout

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

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

The sample code prints False because 20 is not equal to 20.1. However in my application I want to treat these value as equal because of the difference between these values are with in allowable tolerance. So is there any way to provide a custom comparison function for data(not for Key)?

Any help is appreciated.

Edited :
Sorry for the mistake in the code. I copied the code which I tried to find the solution for this problem. The keys must be equal for my scenario.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:46:43+00:00Added an answer on June 1, 2026 at 3:46 am

    If all you care about is equality for the whole container, I would recommend the ::std::equal algorithm. Here’s how:

    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);
    });
    

    If you care about a ‘less than’ relationship, then ::std::lexicographical_compare is what you want. This requires the C++11 lambda feature to work.

    If what you really want are data values that compare equal in a fuzzy way, I present to you a bit of a hack (and something that also requires a couple of C++11 features) fuzzy-double.cpp. I disable ordering comparisons because that would tempt you to stuff these things in ordering containers, and since (2.0 == 2.1) && (2.1 == 2.2), but (2.0 != 2.2), they are not suitable for this purpose.

    #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 does not allow a double, not even const one, to be a template parameter. It does, OTOH, allow pointers and references to objects with external linkage to be template parameters. So if you declare your tolerance as an extern constexpr double you can than use the named tolerance as a template parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following C++ code std::map<std::string, std::vector<std::vector<std::vector<double> > > > details details[string][index][index].push_back(123.5); May
Let's say I have two maps: typedef int Id; std::map<Id, std::string> idToStringMap; std::map<Id, double>
std::map<std::string, int> m; // Can I make assumption that m[NoSuchKey] will return 0? std::cout
Basically I have, typedef map<std::string, set<double> > MAP_STRING_TO_SET; What is the best way to
For an std::map<std::string, std::string> variables , I'd like to do this: BOOST_CHECK_EQUAL(variables[a], b); The
I've got a std::map: std::map<std::string, std::string> I'm passing string literal to find method. Obviously,
let's say I have std::map< std::string, std::string > m_someMap as a private member variable
basically, I've got my Huffman table as std::map<std::string, char> ciMap; Where string is the
unordered_map<std::string, std::string>* Accounts; I have this code up there to initialize from a pointer,
I have got a std::list< std::pair<std::string,double> > , which I know is sorted according

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.