字符串作为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;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM