草庐IT

c++ - 如何用 C++ 编写 "meta if else if.."?

我只是在学习C++元编程的基础知识,我想看看其他人如何解决以下问题会很高兴。此外,很高兴看到使用Boost元编程库的解决方案,因为我认为它们是我的黑暗角落。所以问题是,可以更优雅地重写吗?假设我们有以下结构:templatestructtype_factory{typedeftypenametype_factory_impl::typetype;};这个结构应该是typedeftype,这取决于size的值。type_factory_impl是type_factory的实现。用于确定类型的算法是:if(size%bits::value==0)typedefunsignedlonglon

c++ - 具有相同散列值的值是否在同一个 std::unordered_map 桶中?

如果std::unordered_map的两个键具有相同的哈希值,标准是否保证它们将进入同一个桶?根据模板相等谓词,我们假设键不相等,它们仅具有相同的哈希值。奖励问题:如果相同的散列并不意味着相同的桶,那么能够单独遍历桶的目的是什么? 最佳答案 具有相同哈希值的对象被无序关联容器放入同一个桶中。因此,两个相等的对象必须具有相同的哈希值。23.2.5第8段:Theelementsofanunorderedassociativecontainerareorganizedintobuckets.Keyswiththesamehashcod

c++ - dxvahd.h微软头文件中的#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)什么时候变为true

您好,我有2个VC++解决方案“A”和“B”(VS2008),它们都具有相同的代码库(只有几行代码不同)。在两者中使用DXVAHD.h。dxvahd.h是标准的Microsoft头文件。如果我们打开这个头文件,我们会看到有一个条件if“#ifWINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)”IseethatinVC++solution"A",theaboveconditional#ifstatementisfalse,hencethewholedxvahdheaderfilegetsgreyedout&isnotevencompiled

c++ - 从 std::map 插入/删除元素是否会修改迭代序列?

假设我有以下代码:typedefstd::map::iteratorIterator;Iteratoriter=myMap.begin();while(iter!=myMap.end()){Iteratorcurrent=iter;++iter;maybeDeleteElement(current)//maycallerase.}鉴于std::map是作为红黑树实现的,能否保证映射中的每个元素都恰好被访问一次?还是修改map会导致树重新平衡,从而改变迭代顺序?注意:这不是关于任何迭代器是否会失效的问题。但是保持有效的迭代器并不一定意味着递增它会为您提供与之前相同的下一个元素。

c++ - std::find 和 std::map.find 都是 O(logN) 吗?

std::find和std::map.find都是O(logN)吗?如果是,std::find如何在对数时间内实现对std::map的搜索?std::find的实现是否专门用于std::map用例? 最佳答案 不,std::find是O(N),与容器无关。它不知道“容器”,没有针对std::map的专门化。std::find仅使用迭代器,它没有关于底层容器的信息。根据cppreference.com,实现等同于:templateInputItfind(InputItfirst,InputItlast,constT&value){fo

c++ - 不完整类型 struct std::hash 与 unordered_map 的无效使用,其中 std::pair of enum class 作为键

我想使用unordered_map,std::uint8_t>用于管理一些像素图格式。这里是最少的代码:#include#include#include#include#includeenumclassPNM:std::uint8_t{PBM,PGM,PPM};enumclassFormat:bool{BIN,ASCII};structpair_hash{public:templatestd::size_toperator()(conststd::pair&x)const{returnstd::hash()(x.first)^std::hash()(x.second);}};intma

c++ - 为什么编译器说 : 'enable_if' cannot be used to disable this declaration

templateusingEnable_if=typenamestd::enable_if::type;classDegree;templateconstexprinlineboolIs_Degree(){returnstd::is_base_of::value;}classDegree{public:std::size_tinDeg=0;};templateclassVertex:publicSatellite{public:explicitVertex(intnum):n(num){}private:std::size_tn;};templateclassEdge{public:/

c++ - unordered_map 具有三个元素

我试图在unordered_map中包含三个元素。我试过下面的代码#include#include#include#includetypedefboost::unordered_map>mymap;mymapm;intmain(){//std::unordered_map>m;m.insert({3,std::make_pair(1,1)});m.insert({4,std::make_pair(5,1)});for(auto&x:m)std::cout但是我在打印语句中遇到了很多错误,比如‘std::pair’isnotderivedfrom‘conststd::__cxx11::b

c++ - 使用 enable_if 专门化模板方法

我正在编写一个模板类,它存储一个std::function以便稍后调用它。这是简化的代码:templatestructTest{voidcall(Ttype){function(type);}std::functionfunction;};问题是这个模板不能为void类型编译,因为voidcall(voidtype)变得未定义。将它专门用于void类型并不能缓解问题,因为templatevoidTest::call(void){function();}仍然与call(TType)的声明不兼容。因此,利用C++11的新特性,我尝试了std::enable_if:typenamestd::

c++ - 使用 C++ 模板实现 Haskell 的 `map` 函数的问题

我喜欢使用Haskell,但不得不使用C++来完成学校作业。我正在为C++编写自己的库,它模拟Haskell的Prelude函数,因此如果我愿意,我可以用C++编写更简洁、更实用的风格(repoonGitHub)。我遇到的一个问题是实现类似map的功能对列表进行操作。在Haskell中,String相当于[Char],因此您可以在采用列表的函数中使用字符串。在C++中,std::string不与std::vector是一回事,所以我必须编写多个版本的函数来取std::string或std::vector.这适用于像filter这样的功能或tail因为它们的输入和输出是同一类型。但是用m