草庐IT

c++ - 有效删除 tr1::unordered_map 中的元素

我正在试验tr1::unordered_map并偶然发现了如何解决这个问题有效地删除元素。“删除”方法提供按键删除或通过迭代器。我认为后者更有效率,因为前者大概涉及隐式查找操作。另一方面,我的调查网上有透露迭代器调用后可能会失效insert()方法。我对典型的现实世界情况感兴趣,其中将对象放入哈希表有一个足够长的生命周期,以便在此期间调用insert()生命周期。因此我可以得出结论,在这种情况下,按键删除是唯一的还剩选项?有没有其他方法可以更有效地删除对象?我是充分意识到这个问题只在发生删除的应用程序中很重要经常。我当前的项目是否会出现这种情况,还有待观察,但是我宁愿在设计我的项目时了

c++ - 有没有办法在不删除内容的情况下从 C++ 映射中删除键?

我有一个名为Shape的类和一个ShapeStorage类。ShapeStorage类有一个map...std::mapshapes;还有一个函数...Shape*ReturnShapePointer(intkey){Shape*shape=shapes[key];shapes.erase(key);returnshape;}我的目标是能够让我的主类实例化一个ShapeStorage对象,将一些Shape*存储在形状图中。然后稍后我想从我的map中删除它,但不删除值本身。我希望我的主类仍然能够访问该值。我已经尝试过,我的指针仍然返回正确的值,但我担心因为当我从我的map中删除指针时正在

c++ - 使用以位集为键的 map 时出现问题

我正在尝试使用bitset作为键在C++中创建一个map。但是编译器会生成以下错误消息Infileincludedfrom/usr/include/c++/4.6/string:50:0,from/usr/include/c++/4.6/bits/locale_classes.h:42,from/usr/include/c++/4.6/bits/ios_base.h:43,from/usr/include/c++/4.6/ios:43,from/usr/include/c++/4.6/ostream:40,from/usr/include/c++/4.6/iostream:40,fro

c++ - 是否有 auto_ptr 的替代品可以与 c++11 中的 boost ptr_map 一起使用

在c++11中,auto_ptr已弃用,取而代之的是更合理的unique_ptr。唉,如果你使用boost::ptr_map,auto_ptr就完成了一个非常方便的用途:std::auto_ptrpLayer(newLayer());mRawLayerPtrMap.insert(layerName,pLayer);是否有可能使用与c++11类似的东西。这个我知道Layer*pLayer=newLayer();mFusedLayers.insert(fusedLayerName,pLayer);有效,但auto_ptr在一些更复杂的场景中有它的优点。是否有适用于C++11的替代品?

c++ - std::size 和 std::map 的最大大小?

std::size和std::map的最大大小是多少?有没有办法增加这个数字?谢谢! 最佳答案 您可以通过调用Container::max_size()获得每个标准库容器的最大大小在上面。如果在编译时需要理论最大大小值,请使用std::numeric_limits::max(). 关于c++-std::size和std::map的最大大小?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questi

c++ - std::map::clear 和元素的析构函数

析构函数是否在std::map上被调用?std::map::clear时的元素用来?我尝试调试std::map但看不到std::string析构函数被调用。任何人都可以帮助我理解吗?文档说明它被调用了,但我没有注意到它。 最佳答案 文档是正确的,它确实被调用了。销毁将通过方法std::allocator::deallocate()完成。.在调试器中跟踪它。http://www.cplusplus.com/reference/std/memory/allocator/ 关于c++-std:

c++ - 插入 unordered_map 时没有匹配函数

我声明一个unordered_map如下:boost::unordered_map,t_torrent>torrent_ins;然后向其中插入一个元素(如果键不存在,该映射将返回新元素的引用)t_torrent&torrent_in=torrent_ins[to_array(in)];但是我收到一条错误信息:../src/Tracker/torrent_serialization.cpp:30:instantiatedfromhere/usr/local/include/boost/functional/hash/extensions.hpp:176:error:nomatchingf

c++ - 为什么在使用 std::map::insert() 时编译顺序有时会导致段错误?

我有一个类叫做Controller,在其中,我有一个名为Button的类.Controller包含几个Button不同类型的实例(例如button_type_a、button_type_b)。controller.h#ifndef__controller__#define__controller__classController{public:classButton{public:Button(inttype=-1);private:inttype;};Controller();ButtonA;ButtonB;ButtonX;ButtonY;};#endif按钮类型为ints,我希望能

c++ - STL 允许使用指向不同 map 的迭代器删除 map 的键/值吗?

所以,我偶然发现的是:std::mapmap1;std::mapmap2;map1[2.5]=11;map1[3.5]=12;map2[2.5]=21;map2[3.5]=22;std::map::iteratoriterMap1=map1.find(2.5);//Iwillnowtrytoeraseakey/valuepairinmap2withaniterator//thatpointstomap1.Thisisbad/wrong.ButIamsurprised//thisisallowed.map2.erase(iterMap1);//whatdoyouthinkwouldbep

C++:使用 clang 编译以迭代器作为值的映射时出现巨大错误

我正在尝试将迭代器用作std::map中的值,以便我可以通过对象的id高效地查找对象或通过其有效地迭代对象深度。考虑以下代码:#include#include#include#include#include#include#includestructobject{staticintnext_id;intid;intdepth;std::map::iteratorid_it;std::multimap::iterator>::iteratordepth_it;staticstd::mapby_id;staticstd::multimap::iterator>by_depth;object