【问题标题】:How to implement a concurrent hash table with tbb which maps key to values of different types?如何使用 tbb 实现并发哈希表,将键映射到不同类型的值?
【发布时间】:2015-03-30 21:10:44
【问题描述】:

我的要求是实现一个包含 2 个字段的并发哈希表,第一个是 int 类型的键,而第二个存储整数、char 和结构类型的数据。显而易见的方法是使用

定义哈希映射
typedef concurrent_hash_map<int, void> myTable;

但它给出了以下错误:

/usr/include/c++/4.8/bits/stl_pair.h:102:11: error: instantiation of ‘std::pair<_T1, _T2>::second’ as type ‘void’
    _T2 second;                /// @c second is a copy of the second object
       ^
/usr/include/c++/4.8/bits/stl_pair.h:102:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type
/usr/include/c++/4.8/bits/stl_pair.h:102:11: error: invalid use of ‘void’
/usr/include/c++/4.8/bits/stl_pair.h:112:26: error: forming reference to void
   _GLIBCXX_CONSTEXPR pair(const _T1& __a, const _T2& __b)

是否有解决方案或替代方案?

【问题讨论】:

  • 您可能得到的最接近的是typedef concurrent_hash_map&lt;int, void*&gt; myTable;。或者最好考虑像boost::anyboost::variant 这样的值类型。
  • @πάνταῥεῖ 它显示以下错误:“如果我使用 typedef concurrent_hash_map myTable;

标签: c++ hashmap


【解决方案1】:

你不能这样使用void

你的意思可能是typedef concurrent_hash_map&lt;int, void*&gt; myTable;

如果 boost 在您的项目中可行,您可能最好使用 boost::variant

//assuming myStruct is your "structure"
typedef boost::variant<int,char,myStruct> myValue;
typedef concurrent_hash_map<int, myValue> myTable;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    相关资源
    最近更新 更多