草庐IT

Unordered

全部标签

c++ - 反转或反向迭代 unordered_map

我试图以相反的顺序打印unordered_map的内容,但是它没有rbegin或rend,所以你不能使用reverse_iterator。应该如何反转unordered_map?编辑(来自评论):我希望键按照插入的顺序排列,因此我不能使用map。键似乎保持插入顺序,但我需要将它们颠倒过来。 最佳答案 只需阅读问题中的第一句话即可得出答案:I'mtryingtoprintthecontentsofanunordered_mapinreverseorder您不能按任何顺序打印,因为它是无序的。在无序结构中谈论秩序是没有意义的。unord

c++ - 有效地将 std::unordered_set 的内容移动到 std::vector

在我的代码中,我有一个std::unordered_set,我需要将数据移动到一个std::vector中。我在获取数据时使用std::unordered_set以确保在转换为std::vector之前仅存储唯一值。我的问题是如何最有效地将内容移动到std::vector?移动数据后,我不需要std::unordered_set。我目前有以下内容:std::copy(set.begin(),set.end(),std::back_inserter(vector)); 最佳答案 在C++17之前,你能做的最好的事情是:vector.i

c++ - 自定义类的 unordered_set 是否有默认的哈希函数?

我正在使用std::unordered_set第一次对哈希函数有疑问。据我了解,如果您不指定哈希函数,它将默认为std::hash.我有一个mySet我的一个类(class)的成员:typedefstd::unordered_setUSetType;USetTypemySet;当我尝试构建时,出现以下错误:errorC2440:'typecast':cannotconvertfrom'constMyClass'to'size_t'如果要使用size_t,是否需要定义一个转换函数(到unordered_set)使用自定义类?有什么方法可以避免编写自己的哈希函数而只使用默认值吗?

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++ - 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