I would like to search in a vector depending on "a" or "a and b"
without the overhead of creating a Foo object or another predicate
class.
Is that possible? Using a member function pointer or a global static
function?
Right now I have this, but I don't like it.
------------------------------Example
code----------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
class Foo {
public:
Foo(int aa, int bb, int cc):a(aa),b(bb) ,c(cc) {}
bool operator()(cons t Foo& f) {
return (GetA() == f.GetA() &&
GetB() == f.GetB() &&
GetC() == f.GetC());
}
inline int GetA() const {return a;}
inline int GetB() const {return b;}
inline int GetC() const {return c;}
private:
int a,b,c;
};
class Foo_eq_a : public std::unary_func tion<Foo, bool{
public:
explicit Foo_eq_a(int aa):a(aa) {}
bool operator() (const Foo& f) const
{
return (f.GetA() == a);
}
private:
int a;
};
class Foo_eq_ab : public std::unary_func tion<Foo, bool{
public:
explicit Foo_eq_ab(int aa, int bb):eq_a(aa),b( bb) {}
bool operator() (const Foo& f) const
{
return (eq_a(f) && f.GetB() == b);
}
private:
Foo_eq_a eq_a;
int b;
};
int main()
{
std::vector<Foo foos;
foos.push_back( Foo(1,1,1));
foos.push_back( Foo(1,2,2));
foos.push_back( Foo(1,2,3));
std::vector<Foo >::const_iterat or citer;
citer = std::find_if(fo os.begin(), foos.end(), Foo(1,2,3));
if (citer != foos.end())
std::cout << "operator() hit, okay" << std::endl;
citer = std::find_if(fo os.begin(), foos.end(), Foo(1,2,4));
if (citer == foos.end())
std::cout << "operator() no hit, okay" << std::endl;
citer = std::find_if(fo os.begin(), foos.end(), Foo_eq_ab(1,2)) ;
if (citer == ++foos.begin())
std::cout << "position 1, okay" << std::endl;
system("Pause") ;
}
----------------------------------
Any suggestions?
Thanks
without the overhead of creating a Foo object or another predicate
class.
Is that possible? Using a member function pointer or a global static
function?
Right now I have this, but I don't like it.
------------------------------Example
code----------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
class Foo {
public:
Foo(int aa, int bb, int cc):a(aa),b(bb) ,c(cc) {}
bool operator()(cons t Foo& f) {
return (GetA() == f.GetA() &&
GetB() == f.GetB() &&
GetC() == f.GetC());
}
inline int GetA() const {return a;}
inline int GetB() const {return b;}
inline int GetC() const {return c;}
private:
int a,b,c;
};
class Foo_eq_a : public std::unary_func tion<Foo, bool{
public:
explicit Foo_eq_a(int aa):a(aa) {}
bool operator() (const Foo& f) const
{
return (f.GetA() == a);
}
private:
int a;
};
class Foo_eq_ab : public std::unary_func tion<Foo, bool{
public:
explicit Foo_eq_ab(int aa, int bb):eq_a(aa),b( bb) {}
bool operator() (const Foo& f) const
{
return (eq_a(f) && f.GetB() == b);
}
private:
Foo_eq_a eq_a;
int b;
};
int main()
{
std::vector<Foo foos;
foos.push_back( Foo(1,1,1));
foos.push_back( Foo(1,2,2));
foos.push_back( Foo(1,2,3));
std::vector<Foo >::const_iterat or citer;
citer = std::find_if(fo os.begin(), foos.end(), Foo(1,2,3));
if (citer != foos.end())
std::cout << "operator() hit, okay" << std::endl;
citer = std::find_if(fo os.begin(), foos.end(), Foo(1,2,4));
if (citer == foos.end())
std::cout << "operator() no hit, okay" << std::endl;
citer = std::find_if(fo os.begin(), foos.end(), Foo_eq_ab(1,2)) ;
if (citer == ++foos.begin())
std::cout << "position 1, okay" << std::endl;
system("Pause") ;
}
----------------------------------
Any suggestions?
Thanks
Comment