码迷,mamicode.com
首页 > 其他好文 > 详细

字符串作为map的key

时间:2016-12-06 02:42:59      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:div   include   tor   比较   har   字符串   const   span   nbsp   

#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;
}

 

字符串作为map的key

标签:div   include   tor   比较   har   字符串   const   span   nbsp   

原文地址:http://www.cnblogs.com/zzyoucan/p/6136028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!