草庐IT

Union_map

全部标签

c++ - 未初始化的值由堆分配 : Unordered_map 创建

解决方案:我可以有很大的字符串,然后我必须为它们保留内存。我在哈希表中使用char指针而不是使用字符串,因此我为我的哈希表键保留了适当的内存。问题:如果问题已经提出,我很抱歉,但我找不到任何对我有帮助的答案。我有以下代码:编辑(Valgrind问题函数的主循环)i=0;wordPos=0;for(;it!=end;++it,i++){//Iwanttoignorethiselementonpurposeif(i==1)continue;boolisscript;stringtag(it->tagName());convertToLower(tag);if(it->isTag()==1)

C++总结(7):STL无序容器之unordered_set、unordered_map、unordered_multiset、unordered_multimap详解

前两节介绍了STL中的顺序容器和关联容器,本节来介绍一下无序容器。无序容器与关联容器类似,但是关联容器是顺序排序的,而无序容器实现了未排序(哈希)的数据结构。文章目录1unordered_set2unordered_map3unordered_multiset4unordered_multimap1unordered_set无序集合(unordered_set)是一种使用哈希表实现的无序关联容器,其中键被哈希到哈希表的索引位置,因此插入操作总是随机的。无序集合上的所有操作在平均情况下都具有常数时间复杂度O(1),但在最坏情况下,时间复杂度可以达到线性时间O(n),这取决于内部使用的哈希函数,但

c++ - 在 C++ 中初始化静态 std::map<int, unique_ptr<int>>

这是一个与此post类似的问题.我认为最有前途的答案与模板化静态初始化有关。这是该答案的类(class):templateclasscreate_map{private:std::mapm_map;public:create_map(constT&key,constU&val){m_map[key]=val;}create_map&operator()(constT&key,constU&val){m_map[key]=val;return*this;}operatorstd::map(){returnm_map;}};用法:std::mapmymap=create_map(1,2)(

c++ - 处理 map 键中指向值的常量

我有以下代码:#includeusingnamespacestd;structA{};mapdata;intget_attached_value(constA*p){returndata.at(p);}voidreset_all(){for(constauto&p:data)*p.first=A();}我的问题是,当我在data类型中注释和取消注释const时,此代码因类型错误而失败。有什么方法可以在不使用const_cast并且不丢失get_attached_value中的const的情况下解决这个问题? 最佳答案 问题似乎出在p

c++ - 使用 std::aligned_union 和 std::aligned_union 为小型缓冲区优化别名

我正在研究std::function的小缓冲区优化实现-像对象。Boost实现了boost::function的小缓冲区像这样:unionfunction_buffer{mutablevoid*obj_ptr;structtype_t{constdetail::sp_typeinfo*type;boolconst_qualified;boolvolatile_qualified;}type;mutablevoid(*func_ptr)();structbound_memfunc_ptr_t{void(X::*memfunc_ptr)(int);void*obj_ptr;}bound_m

c++ - Q文件对话框 : GtkDialog mapped without a transient parent

我有一个用C++中的Qt编写的程序的源代码片段,在Linux下运行,它创建一个QFileDialog来打开现有文件。如果我执行此操作,一切似乎都正常,但是当创建对话框时,我收到一条警告说“Gtk-Message:GtkDialogmappedwithoutatransientparent”。在另一个thread我发现调用此函数“gtk_window_set_transient_for()”可修复此错误。但是这个函数是GTK库的一部分但是我使用的是Qt框架。那么有什么解决方案可以解决这个错误吗?这个对话框的父级是一个QMainWindow:QStringfilename=QFileDia

c++ - 从 std::aligned_union_t 获取指向包含对象的指针

我想使用placement-new在std::aligned_union_t中构造一个任意类型的对象。一旦构造成功,我希望能够取回指向构造对象的指针,而不用单独存储它。通过简单地reinterpret_cast'ingstd::aligned_union_t这样做是否合法,只要我确保将其转换为构造的原始类型?下面的示例代码是否合法?MyStruct是否应该满足任何类型特征要求?例如,它必须是POD吗?#include#include#include#includestructMyStruct{intvalue=0;};constexprsize_tc_alignedUnionSize=

c++ - 我如何知道 `rehash` 是否发生在我插入 unordered_map 之后?

我了解unordered_STL容器保留多个桶,桶的数量根据容器中元素的数量而变化。插入时,如果超过一定的限制,容器将重新散列以使用更多的桶,因此每个桶都不太满并且搜索速度更快。这会使迭代器无效。这意味着我不应该将迭代器保存到一个unordered容器中。除了我可以,如果我在重新哈希后更新它们。但是我找不到可靠的方法来检查insert(无论是emplace还是其他)是否导致了重新哈希。我应该监控bucket_count()吗?cppreference表示只有当新的元素数量大于max_load_factor()*bucket_count()时才会发生重新散列。那是有保证的吗?这样做靠谱吗

c++ - 避免使用 std::unordered_map(和其他)进行双重搜索或不必要的构造

我想在std::unordered_map上找到一个带有键的元素,或者如果它不存在则插入它。来自cppreference.com:std::unordered_map::emplaceInsertsanewelementintothecontainerconstructedin-placewiththegivenargsifthereisnoelementwiththekeyinthecontainer....Theelementmaybeconstructedeveniftherealreadyisanelementwiththekeyinthecontainer,inwhichca

union 成员的 C++ 生命周期

在当前版本的C++标准草案中,[basic.life]/1状态:Thelifetimeofanobjectorreferenceisaruntimepropertyoftheobjectorreference.Avariableissaidtohavevacuousinitializationifitisdefault-initializedand,ifitisofclasstypeora(possiblymulti-dimensional)arraythereof,thatclasstypehasatrivialdefaultconstructor.Thelifetimeofanob