草庐IT

mapped_type

全部标签

c++ - 错误 : no match for ‘operator<’ in ‘__x < __y’ when trying to insert in two map

在代码中有两个映射。一个存储对和另一个存储,其中值是具有5个变量的类,数据类型为字符串、整数、字符串、整数、整数。但是在插入第二个映射期间,我收到错误g++错误:尝试在map中插入时,'__x如何解决。classValues{private:std::stringC_addr;intC_port;std::stringS_addr;intS_port;intC_ID;public:Values(std::string,int,std::string,int,int);voidprintValues();};Values::Values(std::stringCaddr,intCport

C++ 重载歧义 : conversion versus promotion with primitive types

在这段代码中:voidf(floatf,longinti){cout有一个歧义。Checkitout!.但是,第二个参数是有符号整数。将int绑定(bind)到longint参数需要提升,但对于float,则需要转换。由于第一个参数是关于两个重载的完全匹配,所以它不算数。但是关于第二个参数,它在第一次过载(提升)上的排名优于在第二个(转化)上的排名。为什么会出现解析歧义,而不是选择第一个重载? 最佳答案 int到long是一个转换。short到int是一种提升。(有关积分促销的完整列表,请参阅[conv.prom]。)同理,floa

c++ - 为 std::unordered_map 中的 const std::reference_wrapper 重载 operator==

我不知道如何使用std::reference_wrapper将std::string引用获取到std::unordered_map中>。根据以下链接,我知道我需要重载operator==。Whycantemplateinstancesnotbededucedin`std::reference_wrapper`s?但是,我不知道如何编写operator==以使其采用conststd::reference_wrapper。如果包装器不是const,那将不是问题。使用char而不是std::string效果很好(不需要重载operator==)。代码:#include#include#inc

c++ - map 中过期的 weak_ptr 会发生什么

我想了解weak_ptr已过期的映射中的条目(类型为boost::weak_ptr)会发生什么。map中的相应条目是否会自动删除?键是一个整数,对应的值是一个weak_ptr。我写的示例代码,但无法编译#include#include#includeusingnamespacestd;classFoo:publicboost::enable_shared_from_this{public:Foo(intn=0):bar(n){std::coutinc_ref(){returnshared_from_this();}private:intbar;};std::map>mappy;intm

力扣报错runtime error: load of null pointer of type ‘int‘解决思路

记录本算法小白刷力扣的这道题遇到的报错349.两个数组的交集https://leetcode.cn/problems/intersection-of-two-arrays/出现报错的代码 /***Note:Thereturnedarraymustbemalloced,assumecallercallsfree().*/int*intersection(int*nums1,intnums1Size,int*nums2,intnums2Size,int*returnSize){inthash[1000]={0};intresult[1000];//交集是去重的,最多只有1000个数for(inti

c++ - 为什么 std::function 没有 function_type 或等效的成员类型?

来自here在我看来std::function没有function_type或等效的成员类型导出用于初始化它的实际类型。它有result_type,argument_type,以及first_argument_type和second_argument_type,但与上述类型完全不同。为什么它不提供这种类型作为其接口(interface)的一部分?肯定会有一个很好的理由,但我不知道是什么原因,所以我很想知道。因为我知道第一个问题是为什么需要它,好吧,想象一下我想做类似std::is_same::value的事情在sfinae评估中检查它们的基础类型是否相同,只要符号相同,它们包含不同的功

c++ - decltype(some_vector)::size_type 不能用作模板参数

下面的类不编译:template,classAllocator=std::allocator>classMyContainer{public:std::vectordata;std::vector>order;};我收到以下编译器错误:error:type/valuemismatchatargument2intemplateparameterlistfor‘templatestructstd::pair’为什么编译失败,而下面的代码工作正常?template,classAllocator=std::allocator>classMyContainer{public:std::vecto

c++ - 接收通用 map 作为参数的模板函数

在很多情况下,我们希望对完全相同的std::map或std::unordered_map执行一些操作,独立于map的类型。让我们考虑以下示例:#include#include#includetemplateclassContainer>voidprintMap(ContainerinputMap,booladditionalParam=false){for(constpairp:inputMap)coutmap1;map1.emplace(a,b);unordered_mapmap2;map2.emplace(a,b);printMap(map1);printMap(map2);ret

c++ - 大条目的慢 std::map

我们有48,16,703个这种格式的条目。1abc2def......4816702blah4816703blah_blah由于条目的数量很大,我担心std::map在插入期间会花费很多时间,因为它需要为每次插入做平衡。仅将这些条目插入map会花费大量时间。我在做map[first]=second;两个问题:1.我在这种情况下使用std::map是否正确?2.我按上面的方式插入是否正确。或者我应该使用map.insert()很抱歉没有做实验并写下绝对数字,但我们希望就我们做的事情是否正确达成普遍共识。此外,它们的键并不总是连续的..附言当然,稍后我们还需要访问该映射以获取与键对应的值。

c++ - std::list 和 std::map 的通用算法?

我有一个感兴趣的类(称之为X)。我有一个std::list(称之为L)。我有一个函数(称之为F)。F(L)根据检查列表中每个X的内部状态的算法返回L的一个子集(一个std::list)。我正在向我的应用程序添加一个std::map(称之为M),我需要定义F(M)以与F(L)相同的方式运行——即也就是说,F(M)也必须返回一个std::list,这是通过检查映射中每个X的内部状态来确定的。作为一个自称懒惰的程序员,我立即看到算法将[逻辑上]相同,并且每种数据类型(std::list和std::map)都是可迭代的模板。我不想两次维护相同的算法,但我不确定如何前进。一种方法是从F(M)中获