谢谢你,我的问题已经解决了。我用了一个类
// A class that used for accept the function parameters
class Equal
{
private:
string keyword;
// The value of keyword
Listing &list;
// The list to add Advertisement*
public:
// Constructor initializes the value of Listing list and string keyword
Equal (Listing &init_list
,const string init_keyword)
: list(init_list) ,keyword ( init_keyword )
{}
// The function call for the element
void operator ( ) ( Advertisement* elem ) const
{
if (elem->getTitle().find(keyword) != -1 ||
elem->getBody().find(keyword) != -1)
{
list.add(elem);
}
}
};
// Return a Listing object that contains those advertisements
// whose name or description contains keyword
Listing Listing::filter(string keyword)
{
if (keyword == "")
return *this;
Listing list;
for_each(this->begin(), this->end(), Equal(list, keyword));
return list;
}