草庐IT

unordered_multiset

全部标签

c++ - 在 C++ 中使用 std::algorithm 输出 `std::multiset` 的唯一元素及其频率(无循环)

我在C++中有以下multiset:templateclassCompareWords{public:booloperator()(Ts1,Ts2){if(s1.length()==s2.length()){return(s1>mySet;typedefstd::multiset>::iteratormySetItr;mySetmWords;我想在集合中打印一次std::string类型的每个唯一元素,然后在我想打印的元素旁边打印它在列表中出现的次数(频率),就像你一样可以看到仿函数“CompareWord”保持集合排序。提出解决方案here,但这不是我需要的,因为我正在寻找不使用(w

c++ - 分配时 unordered_map 更改的顺序

我对这种行为很好奇。我发现分配unordered_map会更改无序映射的内部顺序,而无需任何插入/删除操作:unordered_mapm1;unordered_mapm2;unordered_mapm3;m1[2]="john";m1[4]="sarah";m1[1]="mark";m2=m1;m3=m2;for(autoit=m1.begin();it!=m1.end();++it){coutsecondsecondsecond输出:marksarahjohnjohnsarahmarkmarksarahjohn我知道unordered_map没有维护任何特定的顺序,因为它内部是一个哈

c++ - std::unordered_map<int,float> 是否仍然需要散列整数才能得到值?

我想知道是否std::unordered_map仍然必须对给定的整数进行哈希处理才能得到该值,或者直接使用它。我需要每秒多次快速执行此操作,如std::hash不能保证是身份函数,我将如何重新定义它?(显然不使用STL并编写我自己的容器是可能的,但我怀疑我编写的容器是否会更有效率(可能慢得多,慢得多))。谢谢! 最佳答案 Iwouldliketoknowwhetherstd::unordered_mapstillhastohashthegiveninteger是的。Ineedtoperformthisoperationveryfas

c++ - 如何使用具有多个比较器功能的 std::multiset?

下午好,我有一个C++类Range它实现了一个operator供std::multisetranges_type使用.由于multiset构造函数没有指定自定义比较仿函数,它使用std::lessoperator.但是,我需要为std::multisetranges_type使用第二个比较器仿函数.具体来说,我会指定第二个比较器:std::multiset其中structPointerCompare看起来是这样的:structPointerCompare{booloperator()(constRange&a,constRange&b)const{return(a.mPtr==b.mP

c++ - boost::unordered_map 中的迭代器失效

我正在使用boost::unordered_map如下typedefboost::shared_ptrPtrWriter;typedefstd::listPtrList;boost::unordered_map,PtrList>MapMapinstrMap;现在我正在循环中对PtrList类型的列表进行一些更改for(autoit=instrMap.begin();it!=instrMap.end();++it){autokey=it->first();autolist&=it->second();//Makesomechangetoanelementinlistif(list.emp

c++ - 键不存在时 unordered_multimap::bucket() 的返回值?

当key不存在时,unordered_multimap::bucket(key)是什么应该回来吗?引用资料说它应该返回包含键的桶的桶号,但没有说明当unordered_multimap中不存在该键时会发生什么。Itrieditmyself但我得到的结果不正确:std::unordered_multimapm={{"jack","foo"},{"jill","bar"}};std::cout输出是:jackisinbucket3jillisinbucket4bjarneisinbucket4这是否意味着我必须使用unordered_multimap::count(key)==0来捕获不存

c++ - 当 wordCount 中不存在键时,我应该对 unordered_map<string, int> 使用++wordCount[key] 吗?

见下面的代码:unordered_mapwordCount;for(stringword:words)++wordCount[word];问题:当wordCount中不存在word时,是否可以使用++wordCount[word];?我总是看到有人这样使用,但我不太确定。说明here说:Ifkdoesnotmatchthekeyofanyelementinthecontainer,thefunctioninsertsanewelementwiththatkeyandreturnsareferencetoitsmappedvalue.Noticethatthisalwaysincreas

c++ - 在 std::unordered_map 中使用模板化键

我不明白为什么我的编译器不接受下面的代码#include#includetemplateusingM=std::unordered_set;templateusingD=M;templateusingDM=std::unordered_map::const_iterator//Problem,typenameD::const_iterator>;//Problemintmain(intargc,char**argv){Dd;Mm;DMdm;//Problem}编译命令是clang++-std=c++14test.cpp-otest编译器错误消息摘录是/usr/bin/../lib/gc

c++ - 奇怪的迭代器行为 + unordered_set 的段错误

我有一个类有一个unordered_set成员如下:我有以下类定义,后面是它的常规构造函数和复制构造函数,以及一些其他修改集合的函数(删除了不相关的代码段,因为类很长):#include#include#includeclassHexBoard{public:HexBoard(intn);HexBoard(constHexBoard&obj);std::unordered_setemptyPositions();private:std::unordered_setempty_positions;};HexBoard::HexBoard(intn){for(inti=0;i=nempty

c++ - boost::unordered_map -- 需要指定自定义哈希函数来散列 std::set<int> 吗?

我想使用boost::unordered_map,其中key是std::set.由于一组整数不是内置类型,我假设我必须提供我自己的散列函数(或者,更确切地说,我正在考虑使用boost'shash_range)。但是,现在我尝试像这样初始化散列映射,既不提供散列函数也不提供相等谓词——而且gcc没有提示。这里发生了什么?boost是否足够聪明,可以自行散列所有STL容器?这会比我使用自定义哈希函数慢吗?使用boost::hash_range怎么样??提前致谢。 最佳答案 根据theBoostdocumentation:thedefau