Skip to content

Latest commit

 

History

History
82 lines (47 loc) · 3.02 KB

CppStdEqual_to.md

File metadata and controls

82 lines (47 loc) · 3.02 KB

 

 

 

 

 

 

Predicate to perform operator== on two values.

 

std::equal_to works better on int than on double, because of rounding errors. fuzzy_equal_to can be used for testing two double for equality with some tolerance.

 

 

 

 

 

Example

 

The code below shows how to replace values that are equal to a zero by a one. Note: this is not a preferred version of ReplaceZeroByOne.

 


#include <vector> #include <algorithm #include <numeric> void ReplaceZeroByOne(std::vector<int>& v) {   std::replace_if(v.begin(),v.end(),     std::bind2nd(std::equal_to<int>(),0),1); }

 

 

 

 

 

Another exaple

 


#include <algorithm> #include <cassert> #include <functional> #include <vector> int main() {   //Create a std::vector of 99 zeros and a single 42   std::vector<int> v(99,0);   v.push_back(42);   //Shuffle the std::vector   std::random_shuffle(v.begin(),v.end());   //Count the zeros and 42 using std::find_if and std::bind_2nd   // (though using std::count would be easier)   assert(std::count_if(v.begin(),v.end(),     std::bind2nd(std::equal_to<int>(),0)) == 99);   assert(std::count_if(v.begin(),v.end(),     std::bind2nd(std::equal_to<int>(),42)) == 1); }