C++ map from sortable key

Description

C++ map from sortable key

#include <iostream> 
#include <map> 

using namespace std; 

class Employee /*from   ww  w.j ava  2 s . com*/
{ 
public: 
    string name; 
    string myId; 

    Employee(string aname, string asocial) : 
        name(aname), 
        myId(asocial) {} 

    Employee() : name(""), myId("") {} 
}; 

class Salary 
{ 
public: 
    int v; 
    int v2; 

    Salary(int aannual, int adeductions) : 
        v(aannual), 
        v2(adeductions) {} 

    Salary() : v(0), v2(0) {} 
}; 

bool operator < (const Employee& first, const Employee& second) 
{ 
    return first.name < second.name; 
} 

int main() 
{ 
    map<Employee, Salary> employees; 

    Employee emp1("sparky", "123-22-8888"); 
    Salary sal1(135000, 18); 
    employees[emp1] = sal1; 

    Employee emp2("buzz", "234-33-5555"); 
    Salary sal2(150000, 23); 
    employees[emp2] = sal2; 

    Employee emptest("sparky", ""); 
    cout << employees[emptest].v << endl; 

    return 0; 
}



PreviousNext

Related