草庐IT

c++ - 带有容器和默认分配器的模板模板参数 : can I make my declaration more compact?

我在看这个有趣的话题:https://stackoverflow.com/a/16596463/2436175我的具体案例涉及使用来自opencv的cv::Point_和cv::Rect_的标准容器声明模板函数。我想针对以下模板:我将使用的标准容器类型完成cv::Point_和cv::Rect_定义的基本数据类型我最终做出了以下声明:templateclassContainer_t>voidCreateRects(constContainer_t,std::allocator>>&points,constTvalue,Container_t,std::allocator>>&rects

c++ - 如何从 udp 端点获取 *my* ip

Boost.Asio的udp::endpoint有一个成员是远程地址。因为我在多个接口(interface)上监听(像这样):udp_socket(io_service,udp::endpoint(udp::v4(),port))在我的处理程序中,我不知道哪个网络接口(interface)收到了数据包。如果不遍历网络接口(interface)并在每个接口(interface)上寻找端点地址和我的IP之间的相似性,我能否获得我从中获取消息的接口(interface)的IP? 最佳答案 没有。Boost.Asio不提供识别数据报目标地址

c++ - 如何 std::hash 一个无序的 std::pair

我希望能够使用std::pair作为unordered_container中的键。我知道我可以通过以下方式做到这一点:templatevoidhash_combine(std::size_t&seed,Tconst&key){std::hashhasher;seed^=hasher(key)+0x9e3779b9+(seed>2);}namespacestd{templatestructhash>{std::size_toperator()(std::pairconst&p)const{std::size_tseed(0);::hash_combine(seed,p.first);::

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++ - std::error_code,my_error::check_block == my_error::validate && my_error::accept_block == my_error::validate

我正在使用std::error_code并定义和注册了一堆错误(使用枚举类)。我有一个非常通用的错误,现在称为my_error::validate,但我想在我的库中提供更具体的版本。通常人们会想要使用:if(ec==bc::error::validate)//...但是有时他们可能希望看到与该std::error_code关联的特定错误或打印错误消息。//ec.message()says"check_block()failedtodoXYZ"assert(ec==bc::error::check_block);我希望能够启用如下功能:if(ec==bc::error::validate

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

C++: "my text"是 std::string、*char 还是 c 字符串?

我刚刚做了看起来是acommonnewbiemistake的事情:首先我们阅读oneofmanytutorials是这样的:#includeintmain(){usingnamespacestd;ifstreaminf("file.txt");//(...)}其次,我们尝试在我们的代码中使用类似的东西,它是这样的:#includeintmain(){usingnamespacestd;std::stringfile="file.txt";//Orgetthenameofthefile//fromafunctionthatreturnsstd::string.ifstreaminf(fi

c++ - 为什么 std::hash 不专用于 std::reference_wrapper?

我以为会是这样,但我在我的标准库实现(gcc-4.8.2)中找不到它。为什么std::hash还没有专门用于std::reference_wrapper?#pragmaonce#includenamespacestd{templatestructhash>{size_toperator()(constreference_wrapper&r)const{returnstd::hash()(r.get());}};} 最佳答案 std::reference_wrapper主要用于在默认复制值的实用程序中提供引用语义,例如std::bin