Stores objects that contain employee information to set - C++ STL

C++ examples for STL:set

Description

Stores objects that contain employee information to set

Demo Code

#include <iostream>
#include <set>
#include <string>
using namespace std;
// This class stores employee information.
class employee {/*from   w  w  w .  j a v a  2s . c  o  m*/
  string name;
  string ID;
  string phone;
  string department;
public:
  // Default constructor.
  employee() { ID = name = phone = department = ""; }
  // Construct temporary object using only the ID, which is the key.
  employee(string id) {
    ID = id;
    name = phone = department = "";
  }
  // Construct a complete employee object.
  employee(string n, string id, string dept, string p)
  {
    name = n;
    ID = id;
    phone = p;
    department = dept;
  }
  // Accessor functions for employee data.
  string get_name() { return name; }
  string get_id() { return ID; }
  string get_dept() { return department; }
  string get_phone() { return phone; }
};
// Compare objects using ID.
bool operator<(employee a, employee b)
{
  return a.get_id() < b.get_id();
}
// Check for equality based on ID.
bool operator==(employee a, employee b)
{
  return a.get_id() == b.get_id();
}
// Create an inserter for employee.
ostream &operator<<(ostream &s, employee &o)
{
  s << o.get_name() << endl;
  s << "Emp#:  " << o.get_id() << endl;
  s << "Dept:  " << o.get_dept() << endl;
  s << "Phone: " << o.get_phone() << endl;
  return s;
}
int main()
{
  set<employee> emplist;
  // Initialize the employee list.
  emplist.insert(employee("Tom", "9423",
    "Client Relations", "555-3333"));
  emplist.insert(employee("Susan", "8723",
    "Sales", "555-1111"));
  emplist.insert(employee("Mary", "5719",
    "Repair", "555-2222"));
  // Create an iterator to the set.
  set<employee>::iterator itr = emplist.begin();
  // Display contents of the set.
  cout << "Current set: \n\n";
  do {
    //TODO output code
    ++itr;
  } while (itr != emplist.end());
  cout << endl;
  // Find a specific employee.
  cout << "Searching for employee 8723.\n";
  itr = emplist.find(employee("8723"));
  if (itr != emplist.end()) {
    cout << "Found. Information follows:\n";
    //output code
  }
  return 0;
}

Result


Related Tutorials