草庐IT

mapped_base

全部标签

c++ - 在 hashmap/unordered_map 中,当 value 已经包含 key 时,是否可以避免数据重复

给定以下代码:structItem{std::stringname;intsomeInt;stringsomeString;Item(conststd::string&aName):name(aName){}};std::unordered_mapitems;Item*item=newItem("testitem");items.insert(make_pair(item.name,item);项目名称将在内存中存储两次-一次作为项目结构的一部分,一次作为map条目的键。是否可以避免重复?对于大约100M的记录,这种开销变得巨大。注意:我需要在Item结构中包含名称,因为我使用hash

c++ - 带公历日期的 unordered_map

我想将boost::gregorian::date存储为boost::unordered_map的键,但我无法编译代码,因为它缺少适当的哈希这个类的函数。一个简单的解决方案是转换为std::string并存储它。我可能想避免使用此解决方案,因为使用字符串非常昂贵。我试图找到一些将日期导出为数字的函数,但我只能读取day()函数,我不确定这是否真的合适。也许我可以计算出我的日期和引用日期之间的天数?有没有其他更好的方法来存储日期或将日期导出为数字的函数? 最佳答案 为其实现哈希函数:namespaceboost{namespacegr

c++ - 编译 Qt-base : error: libQt5Core. so.5: 没有可用的版本信息

我是从源代码qt-base编译的。我构建了其中一个测试(tst_qdom),但在执行时出现错误。这是ldd的结果:lddtests/auto/xml/dom/qdom/tst_qdom**tests/auto/xml/dom/qdom/tst_qdom:/usr/lib/x86_64-linux-gnu/libQt5Core.so.5:noversioninformationavailable(requiredbytests/auto/xml/dom/qdom/tst_qdom)tests/auto/xml/dom/qdom/tst_qdom:/usr/lib/x86_64-linux

c++ - 这是正确的行为吗? std::map 迭代器失效

#include#includeintmain(intargc,char**argv){std::mapmap;map.emplace(1,1);autoreverse_iter=map.rbegin();std::coutfirstsecondfirstsecond打印出来:1,12,2根据标准,这真的应该发生吗?我没有接触reverse_iter但它指向的值正在改变。我认为std::map中的迭代器应该可以安全地防止插入。然而,它似乎决定reverse_iter不再指向我告诉它的值,而是指向“此时map末尾发生的任何事情”。更新:更多信息,以防万一:前向迭代器似乎不会发生这种情况(

c++ - 为什么 vector::operator[] 的实现方式与 map::operator[] 不同?

std::vector的operator[]是否有任何理由只返回一个引用而不是插入一个新元素?vector::operator的cppreference.com页面显示hereUnlikestd::map::operator[],thisoperatorneverinsertsanewelementintothecontainer.map::operator[]的页面says"Returnsareferencetothevaluethatismappedtoakeyequivalenttokey,performinganinsertionifsuchkeydoesnotalreadye

c++ - std::map 的用途是什么?

谁能解释一下我从这个使用std::map的简单程序中得到的输出。请注意,我将p插入到map中,但没有插入q但它说它找到了它们,而且还说map中只有1个元素!#include#includestructscreenPoint{floatx=0,y=0;screenPoint(floatx_,floaty_):x{x_},y{y_}{}};booloperatorpositions;intmain(intargc,constchar*argv[]){autop=screenPoint(1,2);autoq=screenPoint(2,1);positions.emplace(p,3);au

c++ - 当键可能不存在时,如何返回对对象的引用(来自 unordered_map)?

假设我有一个对象:classObject{public:Object(std::vectorstuff){}}这些对象中的每一个都只能从类Foo访问:classFoo{public:std::unordered_map_objects;boolgetObjectForId(constint&objectId,Object&rep){boolfound=false;std::unordered_map::const_iteratorgot=_objects.find(objectId);if(got!=_objects.end()){found=true;rep=_objects[obj

C++ : union of two types without virtual base class inheritance

是否可以在不手动创建交集类型的情况下创建两种类型的并集?问题是在我的上下文中交集类是完全没有意义的,所以创建它会使代码用户感到困惑。我的实际案例:我正在描述一个数字硬件模拟器,它是许多模块的分层树状结构:classport;classmodule0{porta,b,c;}classmodule1{portc,d,e;}我需要创建这两种类型的union:classtop_level_module{porta,b,c,d,e;}我想应该有一些技术来创建union类型(这是我要问的问题):classtop_level_module:union_type{//porta,b,c,d,e;}但是

c++ - 沮丧 : why: ‘A’ is an inaccessible base of ‘B’ ?

与此错误消息的其他示例不同,我已经有一个指向A的指针并且想要检索实际的子类。这种安排是一些C++包装的C代码的一部分A是一些PODC结构(whatswhy没有动态转换)而test是C中的一些回调调用C++功能并检索应使用强制转换的正确对象。但是为了防止C++用户代码弄乱C-Baseclass,我希望继承protected。MSVC不会提示这个但g++会提示!?从标准的角度来看,哪一个是正确的?为什么?#includeusingnamespacestd;//plainCstructurestructA{inti;};//someC++WrapperclassstructB:protect

c++ - 在 std::map 中存储结构实例

我正在尝试将一些结构映射到其他一些实例,如下所示:templateclassComponent{public:typedefstd::mapinstances_map;instances_mapinstances;Component(){};Tadd(EntityIDid){T*t=newT();instances[id]=*t;return*t;};};然后我这样使用它:structUnitInfos{intowner_id;inthealth;floatx,y;};classLogicComponent:publicComponent{};问题是当它稍后检索数据时,像这样:comp