map insert: class as key and class as value

青春壹個敷衍的年華 提交于 2019-12-25 08:53:52

问题


I have two class StorageAddress and PersistentChunk. I define a map called SsdChunkMap.

typedef std::map<StorageAddress, PersistentChunk> SsdChunkMap;
SsdChunkMap* _SsdChunkMap;

I want to insert data to map.

StorageAddress _StorageAddress(victim._addr);
PersistentChunk _PersistentChunk(victim._hdr.pos.offs, victim._data,
            victim._hdr.compressedSize);
    _SsdChunkMap->insert(make_pair(_StorageAddress, _PersistentChunk));

And I overload the operator <

  inline bool operator < (StorageAddress const& other) const
    {
        if(attId != other.attId)
        {
            return attId < other.attId;
        }
        if (coords.size() != other.coords.size())
        {
            return coords.size() < other.coords.size();
        }
        for (size_t i = 0, n = coords.size(); i < n; i++)
        {
            if (coords[i] != other.coords[i])
            {
                return coords[i] < other.coords[i];
            }
        }
        if (arrId != other.arrId)
        {
            //note: reverse ordering to keep most-recent versions at the front of the map
            return arrId > other.arrId;
        }
        return false;
    }

_SsdChunkMap->insert(make_pair(_StorageAddress, _PersistentChunk));

So, this is right?

来源:https://stackoverflow.com/questions/39323727/map-insert-class-as-key-and-class-as-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!