Containing Instances and Needing Functions That Compare Them - C++ STL

C++ examples for STL:map

Description

Containing Instances and Needing Functions That Compare Them

Demo Code

#include <iostream> 
#include <map> 

using namespace std; 

class Employee // ww  w . j a va2s  .  co m
{ 
public: 
    string Nickname; 
    string SocialSecurityNumber; 

    Employee(string anickname, string asocial) : 
        Nickname(anickname), 
        SocialSecurityNumber(asocial) {} 

    Employee() : Nickname(""), SocialSecurityNumber("") {} 
}; 

class Salary 
{ 
public: 
    int AnnualRipoff; 
    int IRSDeductionsCheat; 

    Salary(int aannual, int adeductions) : 
        AnnualRipoff(aannual), 
        IRSDeductionsCheat(adeductions) {} 

    Salary() : AnnualRipoff(0), IRSDeductionsCheat(0) {} 
}; 

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

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].AnnualRipoff << endl; 

    return 0; 
}

Result


Related Tutorials