字符串作为map的key

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