草庐IT

hash_equals

全部标签

c++ - 为什么 {} 用于访问 std::hash 中的 operator()?

在阅读用于std::unordered_map的std::hash示例时,我注意到{}正在访问operator()函数。http://en.cppreference.com/w/cpp/utility/hashresult_typeoperator()(argument_typeconst&s)const{result_typeconsth1(std::hash{}(s.first_name));result_typeconsth2(std::hash{}(s.last_name));returnh1^(h2这里使用{}代表什么? 最佳答案

c++ - std::sort with equal elements 给出段错误

我有一个存储指针的容器。我正在尝试根据指针指向的相应对象中的数据成员以非递增顺序对这些指针进行排序。在我的例子中,许多对象可能对该数据成员具有相同的值。下面是一个简短的代码来说明这个问题。对排序函数的调用给出了段错误。奇怪的是,如果我在容器中有16个元素指向具有相同double值的对象,则排序似乎有效。但是,如果我有17个元素指向具有相同值的对象,则会出现段错误。谁能解释一下为什么会这样?#include#include#include//someclassclassA{public:doublea;A(doubleaval);};A::A(doubleaval):a(aval){}/

c++ - hash_map 有多普遍?

hash_map和hash_setheader尚未包含在C++标准中,但它们可作为我最近使用的所有编译器的扩展使用。我想知道在不牺牲可移植性的情况下,在实际代码中我可以在多大程度上依赖这些。我正在从事需要在许多架构和编译器上运行的工具项目,包括:Linux(x86_64、AMD/Intel):GCC、Intel、Portland编译器AIX(强大):GCC、xlCCrayXT系列(AMD):GCC、Portland、Pathscale编译器IBM蓝色基因系列(电源):xlC、GCCSGIAltix(Itanium):Intel编译器Windows:不是真正的优先事项,但随时提供有用的答

C++11 static assert for equality comparable type?

如何static_assert模板类型是C++11中的EqualityComparable概念? 最佳答案 您可以使用以下类型特征:#includetemplatestructis_equality_comparable:std::false_type{};templatestructis_equality_comparable()==std::declval(),(void)0)>::type>:std::true_type{};您将以这种方式进行测试:structX{};structY{};booloperator==(Xcon

c++ - std::hash 模板特化的前向声明

为什么前向声明如下:templatestructstd::hash;用gcc和clang编译失败,但用VisualStudio2015编译?gcc6.1.0(使用coliru):main.cpp:11:34:error:invaliduseoftemplate-name'std::hash'withoutanargumentlisttemplatestructstd::hash;^~~~clang3.8.0(使用coliru):main.cpp:11:29:error:forwarddeclarationofstructcannothaveanestednamespecifiertem

C++ 递归 mpl::equal 问题?

我需要一个支持类型递归的类似于mpl::equal的过程。namespacempl=boost::mpl;BOOST_MPL_ASSERT((mpl::equal,typenamempl::push_back,char>::type>));//OK上面的编译很好,但是如果我在mpl::transform或mpl::fold中使用它,visualstudio2010rc1会提示。typedefmpl::vector,mpl::vector>type_1;typedefmpl::transform,mpl::vector>,mpl::push_back>::typetype_2;BOOST

c++ - 定义 std::hash<std::function>

我需要创建一个模板类,它可以保存指向T类型元素的指针。然后对它们执行功能。这些函数会来自不同的地方,所以我需要一个容器来存储它们,以便以后调用它们。我决定使用std::unordered_set,因为它提供了速度并限制了重复,因为它被实现为哈希表。我编写了整个类,但由于没有为我的std::function定义的散列函数,所以无法编译。它采用T类型的指针并返回void.用struct hash>指定它很容易(并且还重载了()运算符)对于我使用的每种类型,但我实际上如何对函数进行哈希处理?这是我的类(class)中相关成员和方法的精简摘录:templateclassMaster{priva

c++ - 在这种特殊情况下,为什么不需要将 std::hash() 的特化注入(inject)到 std namespace 中?

考虑usingnamespacestd;templatestructhash>{inlinesize_toperator()(constpair&v)const{return0;}};在这种情况下,GCC和Clang都可以正常编译,没有任何警告。然而,这似乎与我在网上读到的内容相矛盾,即定义您自己的哈希函数以与标准库的无序类型一起使用需要您将定义放在std命名空间中。有趣的是,专门针对pair:templatestructhash>{size_toperator()(constpair&v)const{size_tseed=0;returnseed;}};如我们所料导致错误。但是,为什

c++ - 在 Visual Studio 下使用 pair 作为 hash_map 的键

尝试在VisualStudio2010下使用pair作为hash_map的键值。无法编译。int_tmain(intargc,_TCHAR*argv[]){hash_map,int>months;months[pair(2,3)]=1;intd;cin>>d;return0;}收到错误信息:Error1errorC2440:'typecast':cannotconvertfrom'conststd::pair'to'size_t'c:\programfiles\microsoftvisualstudio10.0\vc\include\xhash341testApplication1我知

用于模板类的 C++ std::tr1::hash

我有这个模板类:templateThing{...};我想在unordered_set中使用它:templateclassBozo{typedefunordered_set>things_type;things_typethings;...};现在类Thing拥有它需要的一切,除了哈希函数。我想使它通用,所以我尝试类似的方法:namespacestd{namespacetr1{templatesize_thash>::operator()(constThing&t)const{...}}}尝试用g++4.7编译它时会发出尖叫expectedinitializerbefore‘关于has