字符串作為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