C++/Function/Function Adaptor

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Use a function adaptor in vector

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
using namespace std;
int main()
{
  vector<char *> vectorObject;
  vector<char *>::iterator p;
  int i;
  vectorObject.push_back("One");
  vectorObject.push_back("Two");
  vectorObject.push_back("Three");
  vectorObject.push_back("Four");
  vectorObject.push_back("Five");
  cout << "Sequence contains:";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  cout << "Searching sequence for Three.\n";
  // use a pointer-to-function adaptor
  p = find_if(vectorObject.begin(), vectorObject.end(), not1(bind2nd(ptr_fun(strcmp), "Three")));
  if(p != vectorObject.end()) {
    cout << "Found.";
    cout << "Sequence from that point is:";
    do {
      cout << *p++ << " ";
    } while (p != vectorObject.end());
  }
  return 0;
}


Use a unary function object to determine even/odd.

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class isEven: public unary_function<int, bool> {
public:
  result_type operator()(argument_type i)
  { 
    return (result_type) !(i%2);
  }
};
int main()
{
  vector<int> vectorObject;
  int i;
  for(i = 1; i < 20; i++) 
     vectorObject.push_back(i);
  cout << "Sequence:";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  i = count_if(vectorObject.begin(), vectorObject.end(), isEven());
  cout << i << " numbers are evenly divisible by 2.";
  return 0;
}


Use member function reference function adaptor

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Numbers {
  int val;
public:
  Numbers() { 
     val = 0; 
  }
  Numbers(int x) { 
     val = x; 
  }
  bool showval() { 
     cout << val << " "; 
     return true; 
  }
  bool isPrime() {
    for(int i = 2; i <= (val/2); i++)
      if(!(val%i)) 
         return false;
    return true;
  }
  bool isEven() { 
     return (bool) !(val % 2); 
  }
  bool isOdd() { 
     return (bool) (val %2); 
  }  
};
int main()
{
  vector<Numbers> vectorObject(10);
  vector<Numbers>::iterator end_p;
  int i;
  for(i = 0; i <10; i++)
    vectorObject[ i ] = Numbers(i+1);
  cout << "Sequence contains: ";
  for_each(vectorObject.begin(), vectorObject.end(), mem_fun_ref(&Numbers::showval));
  cout << endl;
  // remove the primes
  end_p = remove_if(vectorObject.begin(), vectorObject.end(), mem_fun_ref(&Numbers::isPrime));  
  cout << "Sequence after removing primes: ";
  for_each(vectorObject.begin(),  end_p, mem_fun_ref(&Numbers::showval));
  cout << endl;
  for(i = 0; i <10; i++)
    vectorObject[ i ] = Numbers(i + 1);
  
  end_p = remove_if(vectorObject.begin(), vectorObject.end(), mem_fun_ref(&Numbers::isEven));
  cout << "Sequence after removing even values: ";
  for_each(vectorObject.begin(), end_p, mem_fun_ref(&Numbers::showval));
  cout << endl;
  for(i = 0; i < 10; i++)
    vectorObject[ i ] = Numbers(i + 1);
  end_p = remove_if(vectorObject.begin(), vectorObject.end(), mem_fun_ref(&Numbers::isOdd)); 
  cout << "Sequence after removing odd values: ";
  for_each(vectorObject.begin(), end_p, mem_fun_ref(&Numbers::showval));

  return 0;
}