- C To filter a class vector using algorithm
- 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"));
- using std::copy_if;
- using std::ostream_iterator;
- using std::cout;
- enum Region {
- AMERICA,
- EUROPE,
- };
- bool is_american(const Student& student)
- {
- return student.isFrom(AMERICA);
- }
- copy_if(students.begin(), students.end(),
- ostream_iterator<Student>(cout, "n"),
- is_american);
- 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); });
- }
- 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, " "));
- }
- 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"));
- 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";
- }
- });
- 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"));