草庐IT

unordered_set

全部标签

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++ - libpng png_set_add_alpha | png_set_filler 错误 : sequential row overflow

我正在尝试使用C读取PNG文件以用于OpenCL。OpenCL不支持24位RGB图像,因此我需要将数据从RGB扩展到RGBA。我使用的PNG都是24位的,因此可以避免头痛。我曾尝试使用png_set_filler和png_set_add_alpha,我认为它们大致相同来解决问题,但它们都会导致此错误:libpngerror:sequentialrowoverflow这里是完整的函数:intLoadPNG24(unsignedchar**pixelBuffer,constchar*filename,unsignedint*width,unsignedint*height){png_byt

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++ - set_intersection 用于两种不同类型的集合

有什么方法可以对两种不同类型的集合执行std::set_intersection吗?我有两套:std::setl_set1;std::setl_set2;我可以为它们定义一些比较器来检查X1和X2是否相等。structsample_comparer{booloperator()(const&X1p_left,const&X2p_right){returnp_left==p_right;}};现在,我尝试对这两个集合进行集合交集:std::setl_intersect;std::set_intersection(l_set1.begin(),l_set1.end(),l_set2.beg

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

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