草庐IT

unordered_multiset

全部标签

c++ - 如何unordered_set<tuple<int,int>>?

我在构建unordeed_set>时遇到了奇怪的问题.我试过VC++8、gcc3.2、gcc4.3,结果都是一样的。我不知道代码有什么问题,以下是我的代码:#include#include//Forunorderedcontainer,thedeclarationofoperator==#includeusingnamespacestd;usingnamespaceboost;//defineofthehash_valuefuncitonfortuplesize_thash_value(tupleconst&t){returnget(t)*10+get(t);}intmain(){un

c++ - 如何散列 unordered_map?

boost::hash具有适用于大多数内置类型(包括容器)的哈希函数。但如boost::hash_rangefunctiondescription中所述,范围的哈希算法issensitivetotheorderoftheelementssoitwouldn'tbeappropriatetousethiswithanunorderedcontainer因此,std::unordered_map和boost::unordered_map都没有boost::hash特化。问题是:是否有一种“简单有效”的方法来散列unordered_map而无需从头开始重新实现散列算法?

c++ - 与 std::unordered_map 的数据竞争,尽管使用互斥锁定插入

我有一个C++11程序,它执行一些计算并使用std::unordered_map来缓存这些计算的结果。该程序使用多个线程,它们使用共享的unordered_map来存储和共享计算结果。基于我对unordered_map和STL容器规范的阅读,以及unordered_mapthreadsafety,似乎一个unordered_map,被多个线程共享,一次可以处理一个线程写入,但是一次可以处理多个读取器。因此,我使用std::mutex来包装我对map的insert()调用,这样最多只有一个线程插入时间。但是,我的find()调用没有互斥锁,因为从我的阅读来看,似乎许多线程应该能够同时读取

c++ - set_union 与 multiset 容器?

当一个或两个输入容器是具有重复对象的多重集时,算法std:set_union的返回值是多少?dups会迷路吗?让我们假设例如:multisetms1;ms1.insert(1);ms1.insert(1);ms1.insert(1);ms1.insert(2);ms1.insert(3);multisetms2;ms2.insert(1);ms2.insert(1);ms2.insert(2);ms2.insert(2);ms2.insert(4);vectorv(10);set_union(ms1.begin(),ms1.end(),ms2.begin(),ms2.end(),v.b

c++ - 具有相同散列值的值是否在同一个 std::unordered_map 桶中?

如果std::unordered_map的两个键具有相同的哈希值,标准是否保证它们将进入同一个桶?根据模板相等谓词,我们假设键不相等,它们仅具有相同的哈希值。奖励问题:如果相同的散列并不意味着相同的桶,那么能够单独遍历桶的目的是什么? 最佳答案 具有相同哈希值的对象被无序关联容器放入同一个桶中。因此,两个相等的对象必须具有相同的哈希值。23.2.5第8段:Theelementsofanunorderedassociativecontainerareorganizedintobuckets.Keyswiththesamehashcod

c++ - 不完整类型 struct std::hash 与 unordered_map 的无效使用,其中 std::pair of enum class 作为键

我想使用unordered_map,std::uint8_t>用于管理一些像素图格式。这里是最少的代码:#include#include#include#include#includeenumclassPNM:std::uint8_t{PBM,PGM,PPM};enumclassFormat:bool{BIN,ASCII};structpair_hash{public:templatestd::size_toperator()(conststd::pair&x)const{returnstd::hash()(x.first)^std::hash()(x.second);}};intma

c++ - 如何迭代 unordered_set 中的无序对?

在unordered_set中迭代无序元素对的简洁方法是什么?(因此顺序无关紧要,元素应该不同)e.g.{1,2,3}=>(1,2)(2,3)(1,3)我最初的尝试是这样的for(i=0;i但是对于迭代器来说这不是super方便。 最佳答案 这应该有效,给定一个std::unordered_sets:autoset_end=s.end();for(autoai=s.begin();ai!=set_end;++ai){for(autobi=std::next(ai);bi!=set_end;++bi){//*ai,*bi}}这基本上是

c++ - unordered_map 具有三个元素

我试图在unordered_map中包含三个元素。我试过下面的代码#include#include#include#includetypedefboost::unordered_map>mymap;mymapm;intmain(){//std::unordered_map>m;m.insert({3,std::make_pair(1,1)});m.insert({4,std::make_pair(5,1)});for(auto&x:m)std::cout但是我在打印语句中遇到了很多错误,比如‘std::pair’isnotderivedfrom‘conststd::__cxx11::b

c++ - 如何使用 const 键复制 unordered_map?

简单代码:#includeintmain(){std::unordered_mapm;std::unordered_mapm1=m;}产生复杂的编译错误信息:ErrorC2280'std::hash::hash(void)':attemptingtoreferenceadeletedfunction在其内部基本上说unordered_map并不期望key是常量附言:我读过answer对于类似的问题:Theassociativecontainersonlyexposethe(key,value)pairasstd::pair,sotheadditionalconstonthekeytyp

c++ - 为什么 vector 比 unordered_map 快?

我正在LeetCode上解决一个问题,但还没有人能够解释我的问题。问题是这样的:给定一个任意的赎金票据字符串和另一个包含来自所有杂志的字母的字符串,编写一个函数,如果赎金票据可以从杂志中构造出来,该函数将返回true;否则,它将返回false。杂志字符串中的每个字母只能在您的赎金记录中使用一次。注意:您可能会假设这两个字符串都只包含小写字母。canConstruct("a","b")->falsecanConstruct("aa","ab")->falsecanConstruct("aa","aab")->true我的代码(耗时32毫秒):classSolution{public:bo