草庐IT

unordered_container

全部标签

c++ - boost::uuids::uuid 作为 std::unordered_map 中的键?

我在MacOSX上使用clang(CXX='clang++-std=c++11-stdlib=libc++'),boost1.53.0。我想在unordered_map中使用uuid作为键,但出现以下错误:/usr/bin/../lib/c++/v1/type_traits:748:38:error:implicitinstantiationofundefinedtemplate'std::__1::hash':publicintegral_constant{};^/usr/bin/../lib/c++/v1/unordered_map:327:54:note:ininstantiat

c++ - insert() 在 map 或 unordered_map 中是必需的吗?

我看到很多通过operator[]将项目添加到map或unordered_map的示例,如下所示:intmain(){unordered_mapm;m["foo"]=42;cout是否有任何理由改用insert成员函数?看起来他们都在做同样的事情。 最佳答案 他们不是。operator[]将覆盖此键的值(如果存在),而insert不会的。万一operator[]用于插入元素,预计速度会慢一些(有关详细信息,请参阅下面@MatthieuM的评论),但这在这里并不重要。同时std::map::insert返回std::pair,其中.s

c++ - 不区分大小写 unordered_map<string, int>

如何创建不区分大小写的unordered_map?是否覆盖key_equal足够了,否则我还需要更新hasher? 最佳答案 Hasher也需要更新,因为默认的哈希算法doesnotproduceidenticalhashcodeforstringsthatdifferonlyinthecaseoftheirsymbols-旨在处理不区分大小写的字符串的哈希码函数的一个基本属性。std::strings1="Hello";std::strings2="hello";std::hashhash_fn;size_thash1=hash_

C++11 unordered_set with std::owner_less-like hashing

我正在使用外部网络库,它返回一些表示打开的套接字的神奇结构,文档说当将它们插入STL容器时,应该使用std::owner_less比较它们。std::map,std::owner_less>sockets;但是我想改用unordered_map。我该怎么做?std::owner_less是一个比较器,它对HashMap毫无用处。挖掘源代码,MagicStructure似乎是std::shared_ptr的类型定义。 最佳答案 不幸的是,您似乎必须使用map,而对于这种情况不能使用unordered_map:http://wg21.c

c++ - (Re)Using std::algorithms with non-standard containers

我有一个“列”容器类型:structMyColumnType{//Data:Eachrowrepresentsamemberofanobject.vectora;//Allvectorsareguaranteedtohavealwaysvectorb;//thesamelength.vectorc;voidcopy(intfrom_pos,intto_pos);//Thecolumntypeprovidesaninterfacevoidswap(intpos_a,intpos_b);//forcopying,swapping,...voidpush_back();//Andforres

c++ - 无法将 std::unorded_set 与自定义 KeyEqual 进行比较

以下程序无法编译。但是如果我不注释掉operator==,它会编译。为什么在我已经提供了FooEqual的情况下仍然需要operator==#include#includestructFoo{};structFooHasher{size_toperator()(constFoo&)const{return1;}};structFooEqual{booloperator()(constFoo&lhs,constFoo&rhs)const{returntrue;}};//booloperator==(constFoo&lhs,constFoo&rhs){//returntrue;//}in

c++ - boost::noncopyable 的 unordered_map 无法从 operator[] 返回引用

为了演示我的问题,请考虑这个无法编译的简单程序:#include#includeclassfoo:boost::noncopyable{};intmain(){std::unordered_mapm;auto&element=m[0];return0;}使用当前版本的boost(1.52),VisualStudio2012返回错误:无法访问类“boost::noncopyable_::noncopyable”中声明的私有(private)成员。std::unordered_map的运算符[]返回对所提供键处元素的引用,乍一看似乎应该有效——我要求的是对元素的引用,而不是它的拷贝.我对这

c++ - 在只有 const shared_ptr 的 unordered_set 中找到一个 shared_ptr?

我有一个unordered_set>us我想知道是否有针k在us,但是k类型为shared_ptr所以unordered_set>::find提示它无法转换。有解决办法吗?也许通过直接提供哈希?我试过const_cast(感觉很脏)但这并没有解决问题。 最佳答案 使用std::const_pointer_cast在这里是一个可能的解决方案。us.find(std::const_pointer_cast(k));因为您没有修改k,所以可以丢弃const。 关于c++-在只有constsha

c++ - std::unordered_map::extract 引用/指针失效

对于新的C++17std::unordered_map::extract函数,文档说:Extractinganodeinvalidatesonlytheiteratorstotheextractedelement,andpreservestherelativeorderoftheelementsthatarenoterased.Pointersandreferencestotheextractedelementremainvalid,butcannotbeusedwhileelementisownedbyanodehandle:theybecomeusableiftheelementi

c++ - 不区分大小写的 STL 容器(例如 std::unordered_set)

制作std::unordered_setCASE-INSENSITIVE容器的最短、最跨平台的方法是什么?my_set.insert("Apples");my_set.insert("apples");//Insertdoesn'toccurbecauseofduplicateitem我知道STL提供了Hash和Pred。Hash应该是什么?Pred应该是什么?如果它们不是内置的,那么请提供它们的代码以及它们的使用示例(即我如何声明std::unordered_set?)。由于批评,我将详细说明我正在尝试做的事情。我需要一个高性能的透明HTTP代理服务器,它所做的其中一件事就是快速查找