SEARCH or FOUND inside of vector problem
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
How to find an item in a std::vector?
考虑到这两类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | class Book { private: string title; int category; public: Book(const string& , int num); } Book(const string& name, int num) :title(name), category(num) { } class Reader { private: string reader_name; vector <Book> bookLists; public: void add(const Book &ref); }; void add(const Book& ref) { // My problem is here. } |
我想在书的矢量中搜索。如果书在书的矢量中,它什么也不做。如果书不在
你应该使用:
1 | std::find(bookLists.begin(), bookLists.end(), bookItemToBeSearched)!=bookLists.end(). |
在这里查看更多关于
1 2 3 4 5 6 7 | void add(const Book& ref) { if(std::find(bookLists.begin(), bookLists.end(), ref)==bookLists.end()) { bookLists.push_back(ref); } } |