【问题标题】:stl unordered_set how to set initial bucket countstl unordered_set 如何设置初始桶数
【发布时间】:2013-03-29 00:56:03
【问题描述】:

对于 unordered_map,我通过以下方式设置初始存储桶计数:

unordered_map<string, dictionaryWord> dictionary(17749); // set initial bucket count to 17749

这种方式似乎不适用于 unordered_set。

/*line 150*/ unsigned char mask;
/*line 151*/ unordered_set<QueryID> query_id(109); // set initial bucket count to 109

编译器错误 = 1) 数字常量之前的预期标识符。 2) 数字常量前应有“,”或“...”。 并继续出现一堆错误

如何对 unordered_set 做同样的事情?

谢谢。

================================================ ==

如果我删除“(109)”,则没有错误

"/usr/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .clean-conf
make[1]: Entering directory `/home/petros/NetBeansProjects/ACM_test'
rm -f -r build/Release
rm -f dist/Release/GNU-Linux-x86/acm_test
make[1]: Leaving directory `/home/petros/NetBeansProjects/ACM_test'


CLEAN SUCCESSFUL (total time: 80ms)

"/usr/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/petros/NetBeansProjects/ACM_test'
"/usr/bin/make"  -f nbproject/Makefile-Release.mk dist/Release/GNU-Linux-x86/acm_test
make[2]: Entering directory `/home/petros/NetBeansProjects/ACM_test'
mkdir -p build/Release/GNU-Linux-x86/ref_impl
rm -f build/Release/GNU-Linux-x86/ref_impl/core.o.d
g++ -pthread -O3 -std=c++11   -c -O2 -pthread -O3 -std=c++11 -MMD -MP -MF build/Release/GNU-Linux-x86/ref_impl/core.o.d -o build/Release/GNU-Linux-x86/ref_impl/core.o ref_impl/core.cpp
ref_impl/core.cpp:151:37: error: expected identifier before numeric constant
ref_impl/core.cpp:151:37: error: expected ‘,’ or ‘...’ before numeric constant
ref_impl/core.cpp: In function ‘ErrorCode StartQuery(QueryID, const char*, MatchType, unsigned int)’:
ref_impl/core.cpp:250:24: error: expected unqualified-id before ‘(’ token
ref_impl/core.cpp:250:38: error: missing template arguments before ‘)’ token
ref_impl/core.cpp:250:39: error: expected ‘;’ before ‘query_id’
ref_impl/core.cpp:257:19: error: ‘query.SameQueries::query_id’ does not have class type
ref_impl/core.cpp: In function ‘ErrorCode EndQuery(QueryID)’:
ref_impl/core.cpp:265:44: error: ‘queries.std::vector<_Tp, _Alloc>::operator[]<SameQueries, std::allocator<SameQueries> >(((std::vector<SameQueries>::size_type)i)).SameQueries::query_id’ does not have class type
ref_impl/core.cpp:265:58: error: unable to deduce ‘const auto’ from ‘<expression error>’
ref_impl/core.cpp:266:38: error: ‘queries.std::vector<_Tp, _Alloc>::operator[]<SameQueries, std::allocator<SameQueries> >(((std::vector<SameQueries>::size_type)i)).SameQueries::query_id’ does not have class type
ref_impl/core.cpp:267:32: error: ‘queries.std::vector<_Tp, _Alloc>::operator[]<SameQueries, std::allocator<SameQueries> >(((std::vector<SameQueries>::size_type)i)).SameQueries::query_id’ does not have class type
ref_impl/core.cpp:268:36: error: ‘queries.std::vector<_Tp, _Alloc>::operator[]<SameQueries, std::allocator<SameQueries> >(((std::vector<SameQueries>::size_type)i)).SameQueries::query_id’ does not have class type
ref_impl/core.cpp: In function ‘void* TaskCode(void*)’:
ref_impl/core.cpp:463:41: error: ‘quer.SameQueries::query_id’ does not have class type
ref_impl/core.cpp:463:48: error: unable to deduce ‘auto’ from ‘<expression error>’
ref_impl/core.cpp:463:70: error: ‘quer.SameQueries::query_id’ does not have class type
make[2]: *** [build/Release/GNU-Linux-x86/ref_impl/core.o] Error 1
make[2]: Leaving directory `/home/petros/NetBeansProjects/ACM_test'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/petros/NetBeansProjects/ACM_test'
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 1s)

【问题讨论】:

  • 应该可以:explicit unordered_set( size_type bucket_count = /*implementation-defined*/, const Hash&amp; hash = Hash(), const KeyEqual&amp; equal = KeyEqual(), const Allocator&amp; alloc = Allocator() );
  • 我们可以看到那些“一堆错误”吗?
  • @scones 你不配!
  • 该死,但我努力! ://
  • 您是否为WID 定义了std::hash 的特化?

标签: c++ c unordered-map bucket unordered-set


【解决方案1】:

它在一个结构中。它应该是:

unordered_set<QueryID> query_id = unordered_set<QueryID>(109);

谢谢

【讨论】:

    【解决方案2】:

    我怀疑桶数是红鲱鱼。看起来QueryID 不可见。

    【讨论】:

    • 如果是这种情况,为什么如果我删除(109)它可以正常工作。它知道QueryID的类型
    • @PetrosDrakoulis:如果在您删除 109 并且不进行其他更改时它有效,我撤回我的建议。
    • @PetrosDrakoulis:如果您包含 SSCCE (sscce.org),它会帮助其他人帮助您。否则未知数太多。
    【解决方案3】:

    我怀疑您的QueryID 没有标准构造函数,即QueryID::QueryID() { std::cout &lt;&lt; "foo!" };,因此无序集不知道如何预填充空 QueryID。

    尝试添加一个。

    或者您可以使用第二个参数来设置要用于每个预建存储桶的默认数据。

    参考: http://en.cppreference.com/w/cpp/container/unordered_set/unordered_set

    【讨论】:

      猜你喜欢
      • 2014-11-14
      • 1970-01-01
      • 2010-12-21
      • 2011-09-02
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 2023-01-30
      • 2013-03-05
      相关资源
      最近更新 更多