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.
If all you care about is equality for the whole container, I would recommend the
::std::equalalgorithm. Here’s how:If you care about a ‘less than’ relationship, then
::std::lexicographical_compareis 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.C++11 does not allow a
double, not evenconstone, 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 anextern constexpr doubleyou can than use the named tolerance as a template parameter.