草庐IT

unordered_container

全部标签

c++ - C++ 中的 `container_of` 宏,具有与 C 相同的签名

我的代码使用著名的container_of宏来实现仅包含宏的链表库。它在C中完美运行。现在我想在它上面支持C++,所以我需要一个container_of替换C++,它匹配以下签名:container_of(ptr,type,member)C实现是这样的:#definecontainer_of(ptr,type,member)({\consttypeof(((type*)0)->member)*__mptr=(ptr);(type*)((char*)__mptr-offsetof(type,member));}) 最佳答案 为自己量身

C++初阶:容器(Containers)vector常用接口详解

介绍完了string类的相关内容后:C++初阶:适合新手的手撕string类(模拟实现string类)接下来进入新的篇章,容器vector介绍:文章目录1.vector的初步介绍2.vector的定义(constructor)3.vector迭代器(iterator)4.vector的三种遍历4.1正常for循环4.2范围for循环4.3两种迭代器(正向和反向)5.vector扩容相关(resize和reserve)5.2reserve()5.2resize()6.vector增删查改6.1push_back和pop_back6.2find、Insert、erase6.3swap1.vecto

c++ - 从模板化模板类和可变模板中声明 "container"对象

我需要声明一个可以存储不同类型容器的类。即,如果它可以处理std::bitset和std::array就好了。但是,这两个类需要不同的模板参数......是否可以(以及可能如何)使用模板化模板类和可变参数模板来声明此类类?示例(但错误):templateclassContainer,std::size_tN,typename...Args>classBase_Class{...Containercontainer;};编译器提示N/2不是类型。显然,对于std::array和std::bitset,我需要将大小作为最后一个模板参数……是否可以编写这种疯狂的代码?谢谢!编辑:就我而言,主

c++ - 使用 unordered_map,其中 Key 是 T 的成员

有没有什么好的方法可以使用unordered_map以便您可以在恒定时间(平均情况下)通过成员变量访问对象?以下示例具有此功能,但需要每个Person的名称复制为key:#include#include#include#includeclassPerson{public:Person():name_(""){}Person(conststd::string&name):name_(name){}std::stringgetName()const{returnname_;}voidkill()const{std::coutmap={{p1.getName(),p1},//Duplicat

Xcode15报错:SDK does not contain ‘libarclite‘ at the path ‘/Applications/Xcode.app/Contents/Developer

报错内容:SDKdoesnotcontain‘libarclite’atthepath‘/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_iphonesimulator.a’;tryincreasingtheminimumdeploymenttarget缺少了libarclite_iphonesimulator.a这个东西,前往文件夹查看:/Applications/Xcode.app/Contents/Developer/Toolchain

c++ - 为什么 unordered_set 操作像计数和删除返回一个 size_type?

显然,unordered_set::erase和unordered_set::count返回一些不是严格bool值的东西(从逻辑上讲,也就是说,我不是在谈论实际类型)。链接页面读取第三个版本的删除:size_typeerase(constkey_type&key);Removestheelementswiththekeyvaluekey这有一种语气,表明可能不止一个元素具有给定的键。它没有明确说明这一点,但听起来很像。现在,集合(即使是无序集合)的要点是每个元素都有一次。标准库承认bool类型的存在并将其用于bool值,如unordered_set::empty().那么,在上述情况下

c++ - 在 C++ 中为 unordered_set 声明散列函数?

这个问题在这里已经有了答案:Isthereadefaulthashfunctionforanunordered_setofacustomclass?(2个答案)Insertingintoanunordered_setwithcustomhashfunction(2个答案)关闭5年前。我必须为一个相当大的项目使用unordered_set,为了确保我正确使用它,我尝试了一个小例子。#include#includeusingnamespacestd;classFoo{private:intx;public:Foo(intin){x=in;}booloperator==(constFoo&f

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++ - 一个通用的 STLish contains()

令我恼火的是,STL容器没有contains()方法返回true如果容器包含元素false否则。所以,我坐下来写了这个:templateinlineboolcontains(constC&container,constE&element){returncontainer.find(element)!=container.end();}对于集合和映射来说效果很好,但对于vector就不行了。或列表。我该如何进行?我应该再写一个吗templateinlineboolcontains(constvector&container,constT&element){std::find(vector