开发者

Using struct as key in hashmap. How to insert values?

The code fail at compilation line

    map_free_segments [ loc ] = color;

The first line for the errors is :

error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const localization'

The complete source :


#include <windows.h>

#include <hash_map>
using namespace std;
using namespace stdext;

#pragma pack(1)
struct localization
{
    char X;
    char Y;
    char Z;
    char L;
};
#pragma pack(1)

typedef hash_map<localization,unsigned long> type_map_free_segments;

//typedef pair<localization, unsigned long> pair_loc;


int main(int argc, CHAR* argv[])
{
    unsigned long color = 1234;
    type_map_free_segments map_free_segments;

    localization loc;

    loc.X = 1;
    loc.Y = 2;
    loc.Z = 3;
    loc.L开发者_如何学Go = 5;

    map_free_segments [ loc ] = color;
    //map_free_segments.insert( pair_loc(loc, color ));

    return 0;
}



To use a complex struct/class as the key in a hash_map, you need to provide an implementation that tells the container how to compare two keys.

Your struct needs to implement the < operator to do that.

You might also need to define a hash_comp function to compute hashes based off of your struct's values.

Look at the help for the hash_compare class for more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜