尝试从std :: map查找const char * key时出错 [英] Error trying to find const char* key from std::map

查看:158
本文介绍了尝试从std :: map查找const char * key时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的地图:

  std :: map< const char *,const char *> map2D; 

映射由API函数的输出填充,返回 const char *

  map2D.insert(std :: pair< const char *,const char * (TempA,TempB)); 

现在有50个 TempA TempB 值,并且有一个键的名称为months。当我搜索这个键,我得到未找到。例如:

  std :: map< const char *,const char *> :: iterator it; 
it = map2D.find(months);

if(it!= map2D.end())
{
std :: cout< Found<第一<< <第二<< '\\\
';
}
else
{
std :: cout< 未找到\\\
;
}

但是当我这样做:

  map2D.insert(std :: pair< const char *,const char *>(months,June); 

我可以找到相应的月份。搜索网络后,我明白这个问题可能与使用<$

解决方案

比较两个 const char * 对于平等不会做你想象的;它比较指针值,而不是指针指向的字符串。你必须提供一个自定义的比较器调用 strcmp ,你不能知道两个字符串文字,即使有相同的字符将存储在同一个地址。

$ d

更好的方式是使用 std :: map< std :: string,std :: string> 没有关系,你的第三方API给你 const char * :你可以简单的构造 std :: string



清除所有权和生存期语义,并自动正确排序。

如果你仍然 需要存储 const char *

/ code>,请注意,这样的要求是非常罕见的,如果您准备好用解释性的注释清理您的容器的安全性;提示:几乎肯定不是。


I have a map declared like this:

std::map<const char*, const char*> map2D;

The map is filled by the output from an API function, which returns const char*:

map2D.insert(std::pair<const char*, const char*>(TempA, TempB));

Now there are 50 TempA values and 50 TempB values, and there is one key with the name of "months". When I am searching for this key, I am getting "not found". E.g.:

std::map<const char*, const char*>::iterator it;
it = map2D.find("months");

if (it != map2D.end()) 
{
    std::cout << "Found " << it->first << " " << it->second << '\n';
}
else 
{
    std::cout << "Not found\n";
}  

But when I am doing it like this:

map2D.insert(std::pair<const char*, const char*>("months", "June");  

I can find the respective month. After searching the web, I understand that this problem may be related to the use of const char*. Can anyone further clarify this?

解决方案

Comparing two const char* for equality does not do what you think it does; it compares pointer values, not the strings that the pointers point to. With string literals this may occasionally "work", but you have no way of knowing that two string literals even with the same characters in it will be stored at the same address. You would have to provide a custom comparator that invokes strcmp, in order to make that work reliably.

You're much better off with a std::map<std::string, std::string>. It doesn't matter that your third-party API gives you const char*: you can simply construct std::strings from those.

This container will have elements with clear ownership and lifetime semantics, and be automatically ordered properly. In short, all your problems will simply go away.

If you still really need to store const char*, be aware that such a requirement is exceedingly rare and should only be even fleetingly considered if you are prepared to litter your code with explanatory comments detailing precisely how and why your container is safe; hint: it almost certainly is not.

这篇关于尝试从std :: map查找const char * key时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆