del*_*316 13 c++ algorithm class vector filter
如何使用算法过滤其国家/地区的类向量studentList?意思我只显示来自"America"国家的学生的详细信息.
bool checkCountry (string x, string y)
{
return (x == y);
}
vector<Student> studentList;
studentList.push_back(Student("Tom", 'M', "91213242", "America"));
studentList.push_back(Student("Jessilyn", 'F', "98422333", "Europe"));
Run Code Online (Sandbox Code Playgroud)
Pet*_*ood 14
using std::copy_if;
using std::ostream_iterator;
using std::cout;
enum Region {
AMERICA,
EUROPE,
REST_OF_WORLD
};
bool is_american(const Student& student)
{
return student.isFrom(AMERICA);
}
copy_if(students.begin(), students.end(),
ostream_iterator<Student>(cout, "\n"),
is_american);
Run Code Online (Sandbox Code Playgroud)
在C++ 11中使用lambda,并允许选择的区域:
void show_students_from_region(const Region& region)
{
copy_if(students.begin(), students.end(),
ostream_iterator<Student>(cout, "\n"),
[&](const Student& student) { return student.isFrom(region); });
}
Run Code Online (Sandbox Code Playgroud)
你可以使用filter_iteratorboost.这是一个示例,底层集合是一个普通的数组.
以下是您可以使用的示例未经测试的代码; 我不得不做出某些假设Student(operator<<对输出有效,国家暴露通过std::string country() const)
struct checkCountry
{
std::string country;
bool operator()(const Student& x)
{
return (x.country() == country);
}
};
int main()
{
std::vector<Student> studentList;
studentList.push_back(Student("Tom", 'M', "91213242", "America"));
studentList.push_back(Student("Jessilyn", 'F', "98422333", "Europe"));
typedef boost::filter_iterator<checkCountry, std::vector<Student>::iterator> FilterIter;
checkCountry predicate;
predicate.country = "America";
FilterIter filter_iter_first(predicate, studentList.begin(), studentList.end());
FilterIter filter_iter_last(predicate, studentList.end(), studentList.end());
std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator<Student>(std::cout, " "));
}
Run Code Online (Sandbox Code Playgroud)
我认为这就是你要找的东西:
struct country_filter
{
country_filter(const std::string& a_country): country(a_country) {}
void operator()(const Student& a_s) const
{
if (country == a_s.country)
{
std::cout << a_s.name << "\n";
}
}
std::string country;
};
//
std::for_each(studentList.begin(), studentList.end(), country_filter("Ireland"));
Run Code Online (Sandbox Code Playgroud)
C++ 11:
std::string country = "America";
std::for_each(studentList.begin(), studentList.end(), [&country] (const Student& a_s)
{
if (a_s.country == country)
{
std::cout << a_s.name << "\n";
}
});
Run Code Online (Sandbox Code Playgroud)
您可以使用实现()运算符的类的对象.这称为仿函数:
struct checkCountry {
const string& compare;
checkCountry(const string& compare) : compare(compare) {}
bool operator()(const string& x) { return x == compare; }
};
vector<Student> studentList;
studentList.push_back(Student("Tom", 'M', "91213242", "America"));
studentList.push_back(Student("Jessilyn", 'F', "98422333", "Europe"));
howMany = std::count_if(studentList.begin(), studentList.end(), checkCountry("America"));
Run Code Online (Sandbox Code Playgroud)
您可以在需要的元谓词任何算法使用仿函数,例如std::count_if,std::find_if等等.