字符串作为map的key

#include <map>
#include <string>
struct cmp_str{
    bool operator()(char const* a, char const* b){
        return std::strcmp(a, b) < 0;//比较字符串的内容
    }
};
int main()
{
    std::map<const char*, int, cmp_str> v;//此时比较的是指针的值,今天差点这样用,如果这样需要自己写比较器
    const char* a = "bello";
    const char* b = "aorld";
    v[a] = 1;
    v[b] = 2;

#if 0
    std::map<std::string, int> v;//此时比较的是内容
    const char* a = "bello";
    const char* b = "aorld";
    v[a] = 1;
    v[b] = 2;
#endif
    return 0;
}

 

posted @ 2016-12-05 23:58  zzyoucan  阅读(5616)  评论(0编辑  收藏  举报