Map with invalid input returning the first inserted value of the map

  c++, dictionary

I created a struct to hold the row/column index of a table

struct itemIndex {
    int row;
    int col;

    bool operator< (const itemIndex &i) const { if (this->col == i.col) return this->row < i.row; else return false; }
    bool operator== (const itemIndex &i) const { return (this->row == i.row && this->col == i.col); }
};

Now, I’ve created a map that holds the index as the key, but only indices in column 1 have created keys. But when calling a value with index having col = 0, it seems to return the first inserted index (row = 0, col = 0) and I’m not sure why. Here’s the implementation of the code:

    itemIndex index;
    index.row = pLVDispInfo->item.iItem;
    index.col = pLVDispInfo->item.iSubItem;
    //example index.row = 5, index.col = 0
    bool found = false; 
    found = m_mSettingMap.find(index) != m_mSettingMap.end(); // returns true
    int val = m_mSettingMap[index];

Source: Windows Questions C++

LEAVE A COMMENT