gpt4 book ai didi

c++ - hash_map 不工作

转载 作者:太空宇宙 更新时间:2023-11-04 16:26:44 26 4
gpt4 key购买 nike

#include <ext/hash_map>

using namespace std;

class hash_t : public __gnu_cxx::hash_map<const char*, list<time_t> > { };
hash_t hash;

...

我在使用这个 hash_map 时遇到了一些问题。用作键的 const char* 始终是一个长度为 12 的数字,格式为 58412xxxxxxx。我知道有 483809 个不同的数字,所以这应该是插入所有内容后的 hash_map 大小,但我只得到 193 个条目。

hash_t::iterator it = hash.find(origen.c_str());
if (it != hash.end()) { //Found

x++;
(*it).second.push_front(fecha);
}
else { //Not found

y++;
list<time_t> lista(1, fecha);
hash.insert(make_pair(origen.c_str(), lista));
}

同样的过程使用 python 字典可以完美地工作(我得到正确的条目数)但使用 c++ 甚至不能关闭。是否有可能因为每个键都以 58412 开头(实际上几乎每个键,但不是全部,这就是我不想砍掉这 5 个字符的原因),我遇到了很多冲突?

最佳答案

const char*对键不利,因为您现在有指针比较而不是字符串比较(另外,您可能有悬空指针,c_str() 的返回值不能长期使用)。

使用 hash_map<std::string, list<time_t> >相反。

关于c++ - hash_map 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10899197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com