2012-01-17 13 views
12

का उपयोग करके कक्षा वेक्टर फ़िल्टर करने के लिए मैं अपने वर्ग द्वारा एल्गोरिदम का उपयोग करने से कक्षा वेक्टर, छात्र सूची कैसे फ़िल्टर कर सकता हूं? मतलब मैं केवल देश "अमेरिका" के छात्रों का विवरण प्रदर्शित करता हूं।सी ++ एल्गोरिदम

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

आप [मिटा-निकालें मुहावरा] की जरूरत है (http://en.wikipedia.org/wiki/Erase-remove_idiom)। –

+1

@ ओली - केवल अगर वह अनमोल प्रविष्टियों को हटाना चाहता था। मैंने उसे यह मतलब लिया कि वह वेक्टर को संरक्षित करना चाहता है, लेकिन केवल कुछ तत्व प्रदर्शित करता है। –

+0

मुझे अन्य प्रविष्टियों को बनाए रखना है ताकि मिट-निकालने काम न करें? – delphi316

उत्तर

7

मैं इस लगता है कि आप के लिए क्या देख रहे:

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

सी ++ 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

आप को बढ़ावा देने से filter_iterator उपयोग कर सकते हैं। अंतर्निहित संग्रह के साथ Here is an example एक सामान्य सरणी है।

नीचे उदाहरण के लिए अनचाहे कोड उदाहरण है; मैं (std::string country() const के माध्यम से उत्पादन, देश उजागर के लिए operator<< वैध) के बारे में Student कुछ मान्यताओं बनाने के लिए

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

आप एक वर्ग है कि () ऑपरेटर को लागू करता है की एक वस्तु का उपयोग कर सकते था।

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

आप उदाहरण के लिए, std::count_if, std::find_if के लिए किसी भी एल्गोरिथ्म है कि एक एकल विधेय की आवश्यकता है में एक functor उपयोग कर सकते हैं, आदि

+1

अमेरिका और यूरोप के देशों के बाद से? – 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); 

सी में एक लैम्ब्डा का उपयोग करते हुए: यह एक functor कहा जाता है ++ 11, और चुने हुए अनुमति देता क्षेत्रों:

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

क्या होगा यदि मेरे मेनू को उपयोगकर्ता को यह चुनने की अनुमति देनी चाहिए कि वह किस देश को फ़िल्टर करना चाहता है? – delphi316

+0

उपरोक्त कोड में कोई त्रुटि है: lambda गायब कैप्चर विनिर्देशक, या तो डिफ़ॉल्ट ([=] या [&]) होना चाहिए या स्पष्ट रूप से क्षेत्र का उल्लेख करें - [क्षेत्र] या [& region]। –

+0

@ माइकलप्लिस्किन धन्यवाद, तय। –

संबंधित मुद्दे