【发布时间】:2016-06-14 07:49:04
【问题描述】:
我使用以下几行创建了一个哈希映射和一个迭代器:
std::map<const char*,vaLueClass *> myCache;
std::map<const char*,vaLueClass *>::iterator myCacheIterator;
然后我使用以下行插入此地图:
myCache[anotherObject->getStringKey()] = new vaLueClass(anotherObj1->getIntAttr(), anotherObj1-->getIntAttr());
然后,每当我尝试使用下面的行搜索此映射或 nut 中是否存在特定字符串的 ENTRY 时,它总是进入 IF 块,换句话说,它在里面找不到任何条目这张地图。
myCacheIterator= myCache.find(sampleObject->getstringKey());
注意:这里sampleObject->getstringKey() 返回之前插入的相同密钥。
if (myCacheIterator.operator ==(myCache.end())){
// this block means that no matched entry is found inside the map myCache
}
另外,这是在 C++ 中创建和使用 std::map 的正确方法吗?如果没有,请推荐一个。
另外,我没有使用关键字new 来创建std::map 对象。
【问题讨论】:
-
你是在比较字符串数据还是指针持有的地址?
-
我想检查此地图中是否存在具有特定 (const char *) 键的条目
-
@user3243499 如果要使用字符串和键,请使用字符串作为键,而不是
const char *。换句话说,使用std::string作为键值。 -
好的,如果我使用
std::string而不是const char *那么请建议我的 IF(condition) 语句的示例替换,以检查我的哈希映射中是否存在特定的字符串键 -
@user3243499 为什么你认为你需要改变什么?
map::find如果您返回的是指向(以空结尾的)字符序列的指针,则可以正常工作。
标签: c++ string c++11 stl omnet++