multiset find使用...

#include <cstdlib> #include <iostream> #include <set> #include <algorithm> #include <iterator> using namespace std; typedef struct _NODE { string name; int id; _NODE(string name, int id) : name(name), id(id){}; friend ostream& operator<< (ostream &os, const _NODE &nod) { return os << "(" << nod.id <<", " << nod.name << ")" << endl; } friend bool operator<(const _NODE &nod, const _NODE &node) { return nod.id < node.id; } }NODE; template<class T> void _printf(const T &begin, const T &end) { ostream_iterator<NODE> os(cout, ""); copy(begin, end, os); cout << endl; } int main(int argc, char *argv[]) { multiset<NODE> mulSet; for(int i=0; i<10; i++) { NODE temp("string", rand() % 10); mulSet.insert(temp); } cout << "all elements in set are: " << endl; _printf(mulSet.begin(), mulSet.end()); int id; cout << "enter a word id to search: "; cin >> id; /*first find method*/ cout << "first method: " << endl; NODE temp("string", id); multiset<NODE>::iterator it = mulSet.find(temp); if(it == mulSet.end()) cout << "no find the word" << endl; else { cout << "find the words: " << endl; multiset<NODE>::size_type count = mulSet.count(temp); for(int i=0; i<count; i++, it++) cout << *it << endl; } /*second find method*/ cout <<"second method: " << endl; multiset<NODE>::iterator beg = mulSet.lower_bound(temp); multiset<NODE>::iterator end = mulSet.upper_bound(temp); if(beg == end) cout << "no find the word" << endl; else { for(beg; beg != end; beg++) cout << *beg << endl; } system("PAUSE"); return EXIT_SUCCESS; }  

 

使用了两种方式实现在multiset中查找的功能...

你可能感兴趣的:(String,struct,OS,search,iterator,include)