草庐IT

map_region

全部标签

c++ - 将自定义类型作为 map 的可能键的可能方法有哪些

这个问题在这里已经有了答案:HowcanIusestd::mapswithuser-definedtypesaskey?(8个答案)关闭8年前。最近有人问我如何将自定义类型作为STL::map的可能键。直截了当我回答他们要重载“customtype链接”当他们问我还有哪些其他方法可以解决这个问题时。我只是暗示我们也可以通过重载“==”运算符来做到这一点。但他对这个答案并不满意。请帮助我知道,是否有任何其他方法可以解决这个问题“将自定义类型作为map的可能键”。

c++ - 无论如何要将键,值,值存储到 map 中

在通读了大部分maps问题之后,我最终从这个链接中得到了一个想法:Howtouniquemydatathatarestoredinanobjectwhicharestoredinavector?我的任务是存储来自用户输入的X、Y、Z坐标。为了防止用户输入重复数据,我决定使用map容器,因为它们不允许重复数据。我测试了代码。我使用X作为key和Y作为value我可以通过以下方式存储X和Y:mapmapp2d;mapp2d.insert(pair(X,Y));并通过此访问它们:map::iteratorp=mapp2d.begin();map::iteratore=mapp2d.end()

C++ Map双线程并发插入读取

有两个线程,一个将插入到map中,另一个将从map中查找。map*mapA;如果线程A将配置对象插入Mapw.r.t字符串键。线程B将尝试使用相同的字符串键查找的位置。如果不存在,它将重试,直到找到字符串键。如果线程A在线程B读取key的同时插入,会不会导致进程崩溃或者数据损坏?这里需要同步吗?在使用示例应用程序进行测试时,我会遇到任何类型的崩溃或损坏 最佳答案 只有当涉及的所有线程都是读取线程时,才可以在没有任何锁定机制的情况下访问容器。这里讨论了STL容器的线程安全:WhydoestheC++STLnotprovideaseto

c++ STL map::operator []在被删除的条目上完成

std::mapbar;intfoo(intkey){bar.erase(key);return1;}intmain(){bar[0]=foo(0);return0;}这段代码是用GCC4.8编译的,在用电围栏检查内存使用情况时会出现段错误。LD_PRELOAD=libefence.so.0.0./a.out问题来自于编译器生成的代码开始在映射中分配一个新条目,然后执行foo()以获取要放入bar[的值0]。在运行foo()时,条目被销毁,代码最终通过写入未分配的内存结束。操作的排序方式取决于编译器实现,还是由C++当前标准指定? 最佳答案

c++ - map 、集合等的 array_view 替代方案

假设我有一些类层次结构,其中有几个virtual返回容器引用的函数:#include#include#include#include#includeclassInterface{public:virtualconststd::vector&getArray()const=0;virtualconststd::set&getSet()const=0;virtualconststd::map&getMap()const=0;};classSubclassA:publicInterface{public:conststd::vector&getArray()constoverride{ret

c++ - 用于 Arduino IDE (xtensa-lx106-elf-gcc) 和 std::map 链接错误的 ESP8266

是否可以将ESP8266的map用于Arduino包?这是我的代码:#includetypedefstd::mapItems;voidsetup(){Itemsitems;items[2]=5;//items.emplace(4,5);}voidloop(){}这是编译/链接错误:Arduino:1.6.5(Windows8.1),Board:"GenericESP8266Module,Serial,80MHz,40MHz,DIO,115200,512K(64KSPIFFS)"sketch_oct31a.cpp.o:Infunction`loop':C:\ProgramFiles(x8

c++ - 对 `boost::iostreams::mapped_file_source::mapped_file_source()' 的 undefined reference

我正在测试boost的内存映射文件,但是一旦我声明了一个boost::iostreams::mapped_file,就像在这个程序中一样:#include//definesff_pipelineandff_Pipe#include#include#include#include#include#include#include#include"MapReduceJob.hpp"usingnamespaceff;intmain(intargc,char*argv[]){boost::iostreams::mapped_filemf;}使用这个makefile:#FastflowandBoo

c++ - std::map 通过变换替换现有元素

我有一个代码:std::vectorvector={1,3,5,7,9};usingmy_type=std::pair;std::map>map;for(constauto&i:vector){map[i]=boost::none;}constmy_typeval={1,5};std::transform(vector.cbegin(),vector.cend(),std::inserter(map,map.end()),[&val](constint&i){returnstd::make_pair(i,boost::optional(val));});一切正常,但std::trans

c++ - 当 wordCount 中不存在键时,我应该对 unordered_map<string, int> 使用++wordCount[key] 吗?

见下面的代码:unordered_mapwordCount;for(stringword:words)++wordCount[word];问题:当wordCount中不存在word时,是否可以使用++wordCount[word];?我总是看到有人这样使用,但我不太确定。说明here说:Ifkdoesnotmatchthekeyofanyelementinthecontainer,thefunctioninsertsanewelementwiththatkeyandreturnsareferencetoitsmappedvalue.Noticethatthisalwaysincreas

c++ - 在 std::unordered_map 中使用模板化键

我不明白为什么我的编译器不接受下面的代码#include#includetemplateusingM=std::unordered_set;templateusingD=M;templateusingDM=std::unordered_map::const_iterator//Problem,typenameD::const_iterator>;//Problemintmain(intargc,char**argv){Dd;Mm;DMdm;//Problem}编译命令是clang++-std=c++14test.cpp-otest编译器错误消息摘录是/usr/bin/../lib/gc