123456789101112131415161718192021222324252627282930313233343536
//store function pointer in vector // anup jan 6, 2015 #include <iostream> #include <vector> using namespace std; bool isGreater(int a, double b) { return a>b ? true:false; } bool isEqual (int a, double b) { return a==b ? true:false; } int main() { bool(*ptf)(int,double) = isGreater; // ptf is a pointer to function cout << ptf(10,12) << endl; vector<bool(*)(int,double)> vect(10); // 10 initial elements vect[0] = isEqual; cout << vect[0](2, 2.0) << endl; vect.push_back( ptf ); // in vect[10] cout << vect[10](4, 3.5) << endl; //same as, cout << ptf(5,3) << endl; vect.push_back( (bool(*)(int,double)) isEqual ); // in vect[11] if(vect[11](2, 1.999999)) cout << "they are equal\n"; else cout << "they are not equal\n"; return 0; }