草庐IT

map_region

全部标签

c++ - std::find 和 std::map.find 都是 O(logN) 吗?

std::find和std::map.find都是O(logN)吗?如果是,std::find如何在对数时间内实现对std::map的搜索?std::find的实现是否专门用于std::map用例? 最佳答案 不,std::find是O(N),与容器无关。它不知道“容器”,没有针对std::map的专门化。std::find仅使用迭代器,它没有关于底层容器的信息。根据cppreference.com,实现等同于:templateInputItfind(InputItfirst,InputItlast,constT&value){fo

c++ - 不完整类型 struct std::hash 与 unordered_map 的无效使用,其中 std::pair of enum class 作为键

我想使用unordered_map,std::uint8_t>用于管理一些像素图格式。这里是最少的代码:#include#include#include#include#includeenumclassPNM:std::uint8_t{PBM,PGM,PPM};enumclassFormat:bool{BIN,ASCII};structpair_hash{public:templatestd::size_toperator()(conststd::pair&x)const{returnstd::hash()(x.first)^std::hash()(x.second);}};intma

c++ - unordered_map 具有三个元素

我试图在unordered_map中包含三个元素。我试过下面的代码#include#include#include#includetypedefboost::unordered_map>mymap;mymapm;intmain(){//std::unordered_map>m;m.insert({3,std::make_pair(1,1)});m.insert({4,std::make_pair(5,1)});for(auto&x:m)std::cout但是我在打印语句中遇到了很多错误,比如‘std::pair’isnotderivedfrom‘conststd::__cxx11::b

c++ - 使用 C++ 模板实现 Haskell 的 `map` 函数的问题

我喜欢使用Haskell,但不得不使用C++来完成学校作业。我正在为C++编写自己的库,它模拟Haskell的Prelude函数,因此如果我愿意,我可以用C++编写更简洁、更实用的风格(repoonGitHub)。我遇到的一个问题是实现类似map的功能对列表进行操作。在Haskell中,String相当于[Char],因此您可以在采用列表的函数中使用字符串。在C++中,std::string不与std::vector是一回事,所以我必须编写多个版本的函数来取std::string或std::vector.这适用于像filter这样的功能或tail因为它们的输入和输出是同一类型。但是用m

c++ - 修改 std::map 的 key

有没有办法修改std::map或的键?Thisexample展示了如何通过重新平衡树来做到这一点。但是,如果我提供一些不需要重新平衡key的保证呢?#include#include#includeclassKeymap{private:intkey;//thiskeywillbeusedfortheindexinginttotal;public:Keymap(intkey):key(key),total(0){}booloperatormy_index;intmain(){std::mapmy_index;Keymapk(2);my_index.insert(std::make_pai

c++ - 为什么 std::map 有一个名为 count 的成员函数?

这个问题在这里已经有了答案:WhydoesSTLsethavecount()whenallelementsaresupposedtobeunique?(1个回答)关闭4年前。我正在学习C++,很明显,一种检查std::map中是否存在特定键的方法是使用成员函数count。但我的第一个想法是:键不应该是唯一的吗?并检查documentation实际上它们是唯一的,因此count将返回0或1。把它叫做count是不是有点违反直觉?为什么不存在?对我来说,在您期望元素出现多次的列表中计数是有意义的,但如果该方法只允许返回1或0,那对我来说就没有意义。我错过了什么吗?是否有理由将其称为coun

c++ - 如何使用 const 键复制 unordered_map?

简单代码:#includeintmain(){std::unordered_mapm;std::unordered_mapm1=m;}产生复杂的编译错误信息:ErrorC2280'std::hash::hash(void)':attemptingtoreferenceadeletedfunction在其内部基本上说unordered_map并不期望key是常量附言:我读过answer对于类似的问题:Theassociativecontainersonlyexposethe(key,value)pairasstd::pair,sotheadditionalconstonthekeytyp

c++ - 通过索引访问 map 值?

我有这张map:m.insert(pair(10,"map1"));m.insert(pair(11,"map2"));m.insert(pair(12,"map3"));m.insert(pair(13,"map4"));m.insert(pair(14,"map5"));然后,我让用户输入一个数字:Pleaseselect:1.Map12.Map23.Map34.Map45.Map5比方说,如果用户输入3,我如何获得值:12?? 最佳答案 使用您当前的设置,没有简单的方法可以做到这一点;您必须遍历map的所有元素,寻找具有Map

c++ - 我怎样才能使 std::find_if 和 std::map 使用一些 boost 库协同工作?

这个问题的灵感来自anothertopic这提出了这个问题:Findthefirstvaluegreaterthanuserspecifiedvaluefromamapcontainer可以通过多种方式解决。典型的C++03解决方案定义了一个专用函数(或仿函数)并将其传递给std::find_if作为第三个参数。在C++11中,可以避免定义专用函数(或仿函数),而是可以使用lambda作为:autoit=std::find_if(m.begin(),mp.end(),[n](conststd::pair&x)->bool{returnx.second>n;});这是theaccepte

c++ - 删除元素时 map 迭代器如何失效?

这个问题在这里已经有了答案:IteratorinvalidationrulesforC++containers(6个答案)关闭3年前。使用erase方法时,迭代器何时以及如何在映射中失效?例如:std::mapaMap;aMap[33]=1;aMap[42]=10000;aMap[69]=100;aMap[666]=-1;std::map::iteratoritEnd=aMap.lower_bound(50);for(std::map::iteratorit=aMap.begin();it!=itEnd;//no-op){aMap.erase(it++);}删除的迭代器肯定会变得无效(