2012-01-17 30 views
12

Làm cách nào để lọc một lớp vectơ, studentList theo quốc gia của nó khỏi sử dụng thuật toán? Có nghĩa là tôi chỉ hiển thị chi tiết của sinh viên từ quốc gia "Mỹ".C++ Để lọc một lớp vector bằng cách sử dụng thuật toán

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")); 
+0

Bạn cần [xóa thành ngữ xóa] (http://en.wikipedia.org/wiki/Erase-remove_idiom). –

+1

@Oli - Chỉ khi anh ấy muốn xóa các mục nhập của người Mỹ. Tôi đã đưa anh ta để có nghĩa là anh ta muốn bảo tồn các vector, nhưng chỉ hiển thị các yếu tố nhất định. –

+0

Tôi phải giữ lại các mục khác để xóa-xóa sẽ không hoạt động? – delphi316

Trả lời

7

Tôi nghĩ rằng đây những gì bạn đang tìm kiếm:

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")); 

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"; 
    } 
}); 
8

Bạn có thể sử dụng filter_iterator từ tăng. Here is an example với bộ sưu tập cơ bản là một mảng thông thường.

Dưới đây là ví dụ về mã chưa được kiểm tra để bạn phát; Tôi phải làm một số giả định về Student (operator<< hợp lệ cho đầu ra, đất nước tiếp xúc qua 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, " ")); 
} 
6

Bạn có thể sử dụng một đối tượng của một lớp mà thực hiện các nhà điều hành (). Đây được gọi là một functor :

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")); 

Bạn có thể sử dụng một functor trong bất kỳ thuật toán đòi hỏi phải có một vị unary, ví dụ, std::count_if, std::find_if vv

+1

Kể từ khi nào là các quốc gia Châu Mỹ và Châu Âu? – aerkenemesis

13
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); 

Sử dụng một lambda trong C++ 11 và cho phép các vùng được chọn:

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); }); 
} 
+0

Điều gì sẽ xảy ra nếu trình đơn của tôi phải cho phép người dùng chọn quốc gia mà họ muốn lọc ra? – delphi316

+0

Có lỗi trong mã ở trên: lambda thiếu thông số nhận dạng chụp, nó phải có mặc định ([=] hoặc [&]) hoặc đề cập rõ ràng vùng - [vùng] hoặc [& vùng]. –

+0

@MichaelPliskin Cảm ơn, đã sửa. –

Các vấn đề liên quan