草庐IT

hash_value

全部标签

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++ - 如何创建具有 64 位输出的良好 hash_combine(受 boost::hash_combine 启发)

目前Boost有hash_combine函数输出32位无符号整数(准确的说是size_t)。一些引用:http://www.boost.org/doc/libs/1_43_0/doc/html/hash/reference.html#boost.hash_combinehttp://www.boost.org/doc/libs/1_43_0/doc/html/hash/combine.htmlMagicnumberinboost::hash_combine我想探索如何创建64位版本的hash_combine。第一件事是在64位中获得黄金比例或任何其他无理数。第二部分是使用轮类。这部分相

C++ 运算符 () 和 'using' 声明 : Left operand must be l-value error

下面的例子说明了一个更复杂但没有什么不同的问题,我一直在努力优雅地解决这个问题。我有一组必须专门化的模板,在这样做时,在每个专门化中实现两个接口(interface)中的一个或两个:可读和可写。Specific实现了这两个接口(interface),然后使用main进行测试:classReadable{protected:intvalues[3];public:Readable(){//Doesnothing.}intoperator()(inti)const{returnvalues[i];}};classWritable:publicReadable{public:Writable

c++ - C++11 中的缩小转换 : what is the "actual value after conversion"?

以下代码在C++11中是否合法?int16_tx{0xaabb};int64_txxxx{0xaaaabbbbccccdddd};代码来自《TheC++ProgrammingLanguage》第4版(第150页)。我们知道,列表初始化是不允许窄化转换的,在标准的窄化转换定义中,我们有:Anarrowingconversionisanimplicitconversion—[...]—fromanintegertypeorunscopedenumerationtypetoanintegertypethatcannotrepresentallthevaluesoftheoriginaltyp

c++ - 有什么能阻止 std::optional::value_or() 有条件地 noexcept 吗?

这是value_or()的定义来自C++17标准:templateconstexprTvalue_or(U&&v)const&;Effects:Equivalentto:returnbool(*this)?**this:static_cast(std::forward(v));Remarks:Ifis_copy_constructible_v&&is_convertible_visfalse,theprogramisill-formed.(右值重载类似)value_or的效果被描述为等同于returnbool(*this)?**this:static_cast(std::forward

c++ - 为什么 `assert` 宏即使与 `NDEBUG` 也有值(value)?

环境:$g++--versiong++(Ubuntu7.4.0-1ubuntu1~18.04)7.4.0众所周知,在包含assert.h之前定义的NDEBUG可以禁用用于调试的类函数宏assert(卡塞特)。但是,在我的环境中阅读/usr/include/assert.h,我发现了下面的代码。#ifdefined__cplusplus&&__GNUC_PREREQ(2,95)#define__ASSERT_VOID_CASTstatic_cast#else#define__ASSERT_VOID_CAST(void)#endif#ifdefNDEBUG#defineassert(exp

没有宏的 C++ 简单反射 : Print Variable Name and Its Value

在C++中是否有一种非宏的方式来打印变量名及其值。这是宏方法:#defineSHOW(a)std::coutPS:我用的是Linux,不需要跨平台的解决方案 最佳答案 不,C++不支持反射,唯一的方法(据我所知)是使用宏。 关于没有宏的C++简单反射:PrintVariableNameandItsValue,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6849965/

c++ - 从 int 转换为 std::array<unsigned char, 1ul>::value_type 可能会改变其值

编译此程序时,-WconversionGCC参数产生标题中的警告:#include#include#includeintmain(){std::stringtest="1";std::arraybyteArray;byteArray[0]=byteArray[0]|test[0];return0;}这是我编译它的方式:g++--Wall-Wextra-Wconversion-pedantic-std=c++0xtest.cpp我使用的是GCC4.5。我在这里做违法的事情吗?它会在某些情况下引起问题吗?为什么|会产生一个int? 最佳答案

c++ - std::hash 特化使用 sfinae?

作为练习,我试图看看我是否可以使用SFINAE为std::pair和std::创建一个std::hash特化:tuple当它的所有模板参数都是无符号类型时。我对它们有一点经验,但据我了解,散列函数需要已经使用typenameEnabled=void进行模板化,以便我添加特化。我不太确定从这里去哪里。这是一个不起作用的尝试。#include#include#include#includenamespacestd{templatestructhash,std::enable_if_t::value>>{size_toperator()(conststd::pair&x)const{retu

c++ - std::hash 模板偏特化

我用模板写了一些类:template>classmy_list;我应该为这个类编写::std::hash特化。我怎样才能做到这一点?简单的偏特化:namespacestd{templateclasshash>{public:size_toperator()(constmy_list&x)const{return...;}};}但是我不能写简单的偏特化,因为它被C++ISO禁止:ISO/IEC14882Thirdedition2011-09-0117.6.4.2.1Namespacestd[namespace.std]2ThebehaviorofaC++programisundefine