where to declare function object class

Z

zs0723

I want to write my own function objects and used in vector<T>
instanstance,
for example

class equalDate
{
string m_date;
public:
equalDate(string date):m_date(date){}

bool operator()(string date)
{
return date == m_date;
}
};


used in another class A's foo() function:
void A::foo()
{

remove_if(vDate.begin(),vDate.end(),qualDate(baseDate));
}

my question is where to declare class "equalDate ", put it in class
A scope
or a separate file ?
 
P

programmer

If you just use the class "equalDate" in class A, you can just
declare it in the class A's header file. Otherwise, you need to
declare it in a separate file.
 
E

Eric Pruneau

zs0723 said:
I want to write my own function objects and used in vector<T>
instanstance,
for example

class equalDate
{
string m_date;
public:
equalDate(string date):m_date(date){}

bool operator()(string date)
{
return date == m_date;
}
};

the class equalDate is useless in your case. The stl already has the
template equal_to functor.
and since string has an operator == you can just type

remove_if(vDate.begin(),vDate.end(), bind2nd(equal_to<string>(), baseDate));
// #include <functionnal> needed here

by the way if you want to erase the elements equal to baseDate, you have to
call erase with the returned iterator of remove_if.

vector<string> NewEnd = remove_if(...);
erase(NewEnd, vDate.end());


Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top