Search code examples
c++stdmap

Nested map detecting second key as duplicate


I have the following code which iterates through a vector of another data type that contains Date,Time and WindLogType

for(unsigned int i = 0;i<holder.size();i++)
    {
        Date date = holder[i].GetDate();
        Time time = holder[i].GetTime();
        WindLogType wlt = holder[i].GetWindLogType();

        DD[date][time] = wlt;

    }

My Map :

typedef map <Time,WindLogType> minuteIntervalData;
typedef map <Date,minuteIntervalData> DayData;

Date contains the date eg. 26/11/2000 Time contains hours and minutes eg. 11:23 WindLogType contains 3 values of type double

However when i try to iterate through the vector to insert into the map, the map detects time as duplicate for the same date

Eg.[26/11/2000][11:23] [26/11/2000][11:25]

These both pair of keys are identified by the map as duplicates and does not insert the value into the map. I had checked my overloaded operator '<' for both Date and Time classes but they seem to be fine

Date :

bool Date::operator < (const Date& D) const
{
    if (m_year < D.GetYear())
    {
        return true;
    }
    if (m_year > D.GetYear())
    {
        return false;
    }
    if (m_month < D.GetMonth())
    {
        return true;
    }
    if (m_month > D.GetMonth())
    {
        return false;
    }
    if (m_day < D.GetDay())
    {
        return true;
    }


}

Time :

bool Time::operator < (const Time& T) const
{
    if (m_hours<T.GetHours())
    {
        return true;
    }
    if (m_hours>T.GetHours())
    {
        return false;
    }
    if (m_minutes<T.GetMinutes())
    {
        return true;
    }
    if (m_minutes>T.GetMinutes())
    {
        return false;
    }

}

Solution

  • For both comparison operators there is an execution path that does not have a return statement. In the first case this would happen if the year and month are the same but the day is greater than the date you compare to, for the second I will leave it to you to figure the case.