标签: unordered-map

g ++链接器错误:为std :: hash获取未定义的引用错误

我在我的代码中使用了TR1实现的unordered_map,并且链接器提供了奇怪的错误,我甚至无法破译:

BPCFG.o: In function `std::__detail::_Hash_code_base<DottedRule, std::pair<DottedRule  const, int>, std::_Select1st<std::pair<DottedRule const, int> >, eqDottedRule,  std::hash<DottedRule>, std::__detail::_Mod_range_hashing,  std::__detail::_Default_ranged_hash, false>::_M_hash_code(DottedRule const&) const':  
BPCFG.cpp:  (.text._ZNKSt8__detail15_Hash_code_baseI10DottedRuleSt4pairIKS1_iESt10_Select1stIS4_E12eqDottedRuleSt4hashIS1_ENS_18_Mod_range_hashingENS_20_Default_ranged_hashELb0EE12_M_hash_codeERS3_[std::__detail::_Hash_code_base<DottedRule,   std::pair<DottedRule const, int>, std::_Select1st<std::pair<DottedRule const, int> >,   eqDottedRule, std::hash<DottedRule>, std::__detail::_Mod_range_hashing,  std::__detail::_Default_ranged_hash, false>::_M_hash_code(DottedRule const&) const]+0x23):  undefined reference to `std::hash<DottedRule>::operator()(DottedRule) const'
BPCFG.o: In function `std::__detail::_Hash_code_base<DottedRule, std::pair<DottedRule  const, int>, std::_Select1st<std::pair<DottedRule const, int> >, eqDottedRule,  std::hash<DottedRule>, std::__detail::_Mod_range_hashing,  std::__detail::_Default_ranged_hash,  false>::_M_bucket_index(std::__detail::_Hash_node<std::pair<DottedRule const, int>, false>  const*, unsigned long) const':
BPCFG.cpp:  (.text._ZNKSt8__detail15_Hash_code_baseI10DottedRuleSt4pairIKS1_iESt10_Select1stIS4_E12eqDottedRuleSt4hashIS1_ENS_18_Mod_range_hashingENS_20_Default_ranged_hashELb0EE15_M_bucket_indexEPKNS_10_Hash_nodeIS4_Lb0EEEm[std::__detail::_Hash_code_base<DottedRule,  std::pair<DottedRule const, int>, std::_Select1st<std::pair<DottedRule const, int> >,  eqDottedRule, std::hash<DottedRule>, std::__detail::_Mod_range_hashing,  std::__detail::_Default_ranged_hash,  false>::_M_bucket_index(std::__detail::_Hash_node<std::pair<DottedRule const, int>, false> …
Run Code Online (Sandbox Code Playgroud)

c++ hash unordered-map linker-errors stdhash

4
推荐指数
1
解决办法
5321
查看次数

如何为不绑定模板参数的tr1 :: unordered_map定义宏/ typedef/etc?

这可能是一个有点愚蠢的问题,但我只需要问一下.我试图在C++中使用unordered_map类,但不是每次都将它作为tr1 :: unordered_map引用,我想只使用关键字hashMap.我知道

typedef tr1::unordered_map<string, int> hashMap 
Run Code Online (Sandbox Code Playgroud)

但是这样可以修复键的数据类型和hashMap对应的值,而我希望有更多如下所示:

#define hashMap tr1::unordered_map
Run Code Online (Sandbox Code Playgroud)

我可以在哪里定义键的数据类型和值取决于要求,但这不起作用.以前有人遇到过这个问题吗?

谢谢

c++ xcode stl unordered-map hashmap

4
推荐指数
1
解决办法
1439
查看次数

C ++从3个浮点值生成unordered_map的键

我真的不明白:

我正在读取点,每个点都从二进制文件中保存3个浮点值。将此点保存在unordered_map中

因此,我尝试从以下3个float值中创建一个键:

第一个意图:仅使用确切的位作为键:

unordered_map<string, vector<float>> points;
string vecToKey( float* a ) {
char bytes[12];
memcpy(bytes, a, 12);
return string(bytes);
}
Run Code Online (Sandbox Code Playgroud)

关键是我肯定想以这种方式消除相同的观点,但是

在示例项目中,读取的结果约为21374点,地图结果大小= 10640点

使用以下方法作为密钥创建的结果是10687分的正确结果

string vec3ToKey( float a[3] ) {
float a1[3];
a1[0] = a[0];
a1[1] = a[1];
a1[2] = a[2];
stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << a1;
return ss.str();
}
Run Code Online (Sandbox Code Playgroud)

问题是速度。第二种方法大约需要16秒,第一种方法只需要1-2秒...我无法自我解释为什么甚至会有差异...

我感谢每个主意:)

c++ floating-point unordered-map key

4
推荐指数
1
解决办法
2023
查看次数

使用unordered_map移动构造函数

我有一些代码:

Class A{
//...A has a move ctor here.
};

unordered_map<int, A> bla;
A tmp;
//operations on tmp
bla.insert(make_pair<int, A>(1, move(tmp)));
Run Code Online (Sandbox Code Playgroud)

我想调用移动构造函数而不是复制类A的ctor.这段代码是否正确?我认同.奇怪的是它编译并适用于Ubuntu Precise(g ++ show version of 4.6.3).但是在CentOS上,它无法编译.前几行是:

 In substitution of ‘template<class _From1, class _To1> static decltype     ((__test_aux<_To1>(declval<_From1>()), std::__sfinae_types::__one()))     std::__is_convertible_helper<_From, _To, false>::__test(int) [with _From1 = _From1; _To1 = _To1; _From = const A&; _To = A] [with _From1 = const A&; _To1 = A]’:
/gcc/x86_64-redhat-linux/4.7.1/../../../../include/c++/4.7.1/type_traits:1258:70:   required from ‘constexpr const bool std::__is_convertible_helper<const A&, A, false>::value’
/gcc/x86_64-redhat-linux/4.7.1/../../../../include/c++/4.7.1/type_traits:1263:12:   required from ‘struct std::is_convertible<const A&, …
Run Code Online (Sandbox Code Playgroud)

c++ linux gcc unordered-map c++11

4
推荐指数
2
解决办法
4272
查看次数

为什么我机器上的hash_map和unordered_map非常慢?

我用这段代码测试了它们(在Visual Studio 2010 sp1上):

#include <ctime>
#include <iostream>
#include <map>
#include <unordered_map>
#include <hash_map>

int main()
{ 
    clock_t time;
    int LOOP = (1 << 16);
    std::map<int, int> my_map;
    std::unordered_map<int, int> map_unordered_map;
    std::hash_map<int, int> my_hash_map;

    time = clock();
    for (int i = 0; i != LOOP; ++i)
    {
        my_map[i] = i;
    }
    std::cout << "map: " << ((double)(clock() - time) / CLOCKS_PER_SEC) << std::endl;

    time = clock();
    for (int i = 0; i != LOOP; ++i)
    {
        map_unordered_map[i] = i;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ unordered-map hashmap map

4
推荐指数
1
解决办法
1685
查看次数

插入unordered_map时没有匹配的功能

我声明unordered_map如下:

boost::unordered_map<std::array<char, 20>, t_torrent> torrent_ins;
Run Code Online (Sandbox Code Playgroud)

然后在其中插入一个元素(如果该键不存在,此映射将返回新元素的引用)

t_torrent& torrent_in = torrent_ins[to_array<char,20>(in)];
Run Code Online (Sandbox Code Playgroud)

但是我收到了一条错误消息:

../src/Tracker/torrent_serialization.cpp:30:   instantiated from here/usr/local/include/boost/functional/hash/extensions.hpp:176: error: no matching function    for call to ‘hash_value(const std::array<char, 20ul>&)’
Run Code Online (Sandbox Code Playgroud)

你能帮我解释一下这个错误吗?非常感谢!

c++ hash boost unordered-map

4
推荐指数
1
解决办法
1394
查看次数

在unordered_map中设置unordered_set

如何将(静态定义的)unordered_set添加到unordered_map,而不必复制unordered_set?

我试过这个:

std::unordered_map<int, std::unordered_set<std::string>> my_map;
for (int i=0; i<100; i++)
  my_map.emplace(i, {"foo", "bar"});
Run Code Online (Sandbox Code Playgroud)

还有这个:

std::unordered_map<int, std::unordered_set<std::string>> my_map;
for (int i=0; i<100; i++)
  my_map.insert(i, std::move(std::unordered_set<std::string>({"foo", "bar"})));
Run Code Online (Sandbox Code Playgroud)

但是没有一个编译,我得到这些错误(分别):

error: no matching function for call to ‘std::unordered_map<int, std::unordered_set<std::basic_string<char> > >::emplace(int&, <brace-enclosed initializer list>)’
Run Code Online (Sandbox Code Playgroud)

error: no matching function for call to ‘std::unordered_map<int, std::unordered_set<std::basic_string<char> > >::insert(int&, std::remove_reference<std::unordered_set<std::basic_string<char> > >::type)’
Run Code Online (Sandbox Code Playgroud)

c++ unordered-map move c++11 emplace

4
推荐指数
1
解决办法
1612
查看次数

无序的地图与矢量

我正在构建一个小的2D游戏引擎.现在我需要存储游戏对象的原型(所有类型的信息).一个容器最多我猜几千个元素都有唯一的键,没有元素将被删除或在第一次加载后添加.键值是一个字符串.

将运行各种线程,并且我需要向每个人发送一个键(或索引)并且具有该访问权限的其他信息(例如用于渲染过程的纹理或用于混合器过程的声音)仅可用于那些线程.

通常我使用向量,因为它们访问已知元素的速度更快.但我发现,如果我使用::at元素访问,无序地图通常也会保持恒定的速度.这将使代码更清晰,也更容易维护,因为我将处理更易理解的人造字符串.

所以问题是,与a vector[n]相比,访问a 与a 之间的速度差异unorderedmap.at("string")与他的收益相比可以忽略不计?

根据我的理解,在程序的不同部分访问各种地图,不同的线程只为我运行"名称"是一个大问题,速度差异不是那么大.但我太缺乏经验,无法确定这一点.虽然我发现有关它的信息似乎我无法理解我是对还是错.

感谢您的时间.

c++ unordered-map vector c++11

4
推荐指数
2
解决办法
9901
查看次数

为什么std :: unordered_map :: count上没有`noexcept`说明符?

我正在阅读关于C++的参考页面std::unordered_map.这些 emptysize方法是noexcept合格的,但不是count.

我不认为它应该投入count.

我错过了什么吗?

c++ unordered-map language-lawyer noexcept c++11

4
推荐指数
1
解决办法
588
查看次数

std :: unordered_map不释放内存

I'm observing odd behavior of std::unordered_map in MSVC14 (VS2015). Consider following scenario. I create an unordered map and fill it with dummy struct which consumes considerable amount of memory, lets say 1Gb, overall 100k elements inserted. Then you start to delete elements from the map. Lets say you have deleted half of elements, then, you expect half of memory being freed. Right? Wrong! I see that memory is released when number of elements in map pass some threshold, in my …

c++ memory-management stl unordered-map visual-c++

4
推荐指数
2
解决办法
4359
查看次数