C++ set Look up custom object
#include <iostream> #include <set> using namespace std; class Employee//from www.jav a 2 s .co m { public: string name; string myId; Employee(string aname, string asocial) : name(aname), myId(asocial) {} Employee() : name(""), myId("") {} }; bool operator < (const Employee& first, const Employee& second) { return first.myId < second.myId; } ostream& operator << (ostream &out, const Employee &emp) { cout << "(" << emp.name; cout << "," << emp.myId; cout << ")"; return out; } int main() { set<Employee> employees; Employee emp1("sparky", "123-22-1111"); employees.insert(emp1); Employee emp2("buzz", "234-33-2222"); employees.insert(emp2); Employee emp3("coollie", "123-22-3333"); employees.insert(emp3); Employee emp4("sputz", "199-19-0000"); employees.insert(emp4); // List the items set<Employee>::iterator iter = employees.begin(); while (iter != employees.end()) { cout << *iter << endl; iter++; } // Find an item cout << "Finding..." << endl; Employee findemp("", "123-22-8572"); iter = employees.find(findemp); cout << *iter << endl; return 0; }