草庐IT

unordered-set

全部标签

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

c++ - Eigen::aligned_allocator 因 std::unordered_multimap 而失败

我正在尝试在XCode6中编译这段代码:std::unordered_multimap,std::equal_to,Eigen::aligned_allocator>>trackingFailed;它失败了:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/unordered_map:1461:5:Static_assertfailed"Invalidallocator::value_type"Eigen3.2.2还需要用aligned_al

c++ - 具有 std::vector 和 std::set 属性的容器?

C++世界中是否存在具有这些属性的容器?元素是独一无二的,并在可定制比较器的帮助下有序提供随机接入运营商。我目前正在将我的数据收集到std::set中然后做一个std::copy(_set.begin(),_set.end(),std::back_inserter(_vec))能够随机访问有序集合。然而,规模可能会达到数亿。 最佳答案 如果可以选择Boost,请查看flat_setintheContainerslibrary.flat_set的接口(interface)与std::set相同但它提供随机访问迭代器,如std::vec

c++ - 无效的模板相关成员函数模板推导 - 认为我正在尝试使用 std::set

我有一个继承自基类模板的类模板。基类模板有一个数据成员和一个我想从父类(superclass)中调用的成员函数模板。我知道为了消除对成员函数模板的调用的歧义,我必须使用template关键字,我必须明确提及this在父类(superclass)中。this->base_member_obj.templatemember_function();这一切都很好,只是我使用的代码库犯了一个相当不幸的错误,即导入了整个namespacestd。,我试图调用的模板成员函数称为set.框架中的某处std::set包含在内,这导致GCC认为我正在尝试声明std::set而不是调用成员函数set.GCC

c++ - unordered_map - {{key,value},{key,value}} 语法无效

我正在尝试编译thecodetakenfromhere//constructingunordered_maps#include#include#includetypedefstd::unordered_mapstringmap;stringmapmerge(stringmapa,stringmapb){stringmaptemp(a);temp.insert(b.begin(),b.end());returntemp;}intmain(){stringmapfirst;//emptystringmapsecond({{"apple","red"},{"lemon","yellow"}}