stdmap

Inserting into std::map while iterating over it?

孤街浪徒 提交于 2020-06-27 08:16:21
问题 I have a Map over which I iterate like this: std::map<unsigned int, GameObject *>::iterator itr = _gameObjects.begin(); while (itr != _gameObjects.end()) { itr->second->Update(); itr++; } Update() might insert an element into the map or even remove one from it, but it doesn't necessarily do any of the two. It obviously doesn't work like that. Is there a way it can be done? 回答1: From std::map::erase(): References and iterators to the erased elements are invalidated. Other references and

How does the compare function in std::map in C++ work if it is reflexively true?

ⅰ亾dé卋堺 提交于 2020-05-25 08:27:28
问题 I have a map in my project. Every time I insert a new element, I want to ensure that the key of the new element I insert is at least a minimum width apart from other elements in the map. To do this I wrote a custom compare class like this: class PulseCompare { public: PulseCompare(int minwidth_):minwidth(minwidth_){}; bool operator()(const int x, const int y) const { if(abs(x-y)>minwidth) return false; else return true; } private: int minwidth; }; and created the map like this: std::map<int

Using STL containers for boost::interprocess::managed_shared_memory

孤街醉人 提交于 2020-01-21 18:53:51
问题 Consider the following situation: class Helper { public: // Getters and setters are present! private: int i; std::map<int, boost::interprocess::managed_shared_memory> shm; } int main() { boost::interprocess::managed_shared_memory shmInfo(boost::interprocess::open_or_create, "Test", 1024); boost::interprocess::map<int, Helper> myMap = shmInfo.construct< boost::interprocess::map<int, Helper> >("Memory"); } myMap (which is a map of int and Helper ) is constructed on the shared_memory. In turn, I

C++ Boost binary serialization of std::map containing pointers constructed from Boost object_pool

廉价感情. 提交于 2020-01-16 16:58:49
问题 My application has a class "MyClass". It's objects are being constructed from the Boost Object_pool. I need to serialized/de serialize a std::map containing these objects as value via Boost Binary Serialization. For Serialization - I take a pointer from the pool, do some operations, insert it in the std::map and serialize it via Boost binary serialization. For De-serialization - I fetch that serialized buffer and de-serialize it with Boost binary serialization. De-serialization happens

How to use struct as key in std::map

点点圈 提交于 2020-01-13 09:53:50
问题 I want to use a std::map whose key and value elements are structures. I get the following error: error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const GUID I understand that I should overload operator < for that case, but the thing is I don't have access to the code of the structure I want to use ( GUID structure in VC++). Here's the code snippet:

How to use struct as key in std::map

…衆ロ難τιáo~ 提交于 2020-01-13 09:52:10
问题 I want to use a std::map whose key and value elements are structures. I get the following error: error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const GUID I understand that I should overload operator < for that case, but the thing is I don't have access to the code of the structure I want to use ( GUID structure in VC++). Here's the code snippet:

std::map calls default constructor on [], copy constructor on insert()

家住魔仙堡 提交于 2020-01-05 06:40:12
问题 My std::map is called uniformBlocks. I was testing out the way to add new elements when I noticed something weird. When I add new key, value pair using the method below: uniformBlocks["MatrixBlock"] = matrixBlock; The default constructor is called. However, when I use insert, the copy constructor is called, which is expected. uniformBlocks.insert( std::pair<const std::string, glWrapper::UBO>("MatrixBlock", matrixBlock) ); Why is there a difference between the two methods. Aren't they

Is there a better (more efficient) way to find if a string can be formed from chars of another string?

こ雲淡風輕ζ 提交于 2020-01-05 05:51:30
问题 This is interesting because it's a possible interview question, so it would be good to know the most efficient algorithm for this problem. I came up with a solution (which contains elements of other people's solutions) that requires a map<char, int> to store letters from the first string as keys, and numbers of their occurrences as values. The algorithm then looks through each letter in the container string and checks if there's already an entry in the map. If so, decrement its value until it

How to use emplace() in a std::map whose value is a std::set (map from something to a set)?

懵懂的女人 提交于 2020-01-02 07:18:51
问题 The Problem I have a std::map<int, std::set<int>> named misi . I'm wondering why misi.emplace(2345, {6, 9}); and misi.emplace({2345, {6, 9}}); don't work as expected, as shown below. The Code #include <set> // std:set #include <map> // std::map #include <utility> // std::piecewise_construct, std::pair #include <tuple> // std::forward_as_tuple #include <iostream> // std::cout, std::endl int main() { // --- std::set initializer list constructor --- std::set<int> si({42, 16}); std::cout << "si

Segmentation fault in std::map::insert(…)

我们两清 提交于 2020-01-02 03:24:14
问题 i've used search but i didn't find answer satisfying me... so.. this is chunk of code: //VoteContainer.h typedef uint32_t order_id_t; typedef int driver_id_t; class Vote { public: enum DriverVoteResponse {YES, NO, TIMEOUT}; struct DriverResponse { driver_id_t driver_id; time_t time; DriverVoteResponse response; }; Vote() : m_order_id(0), m_time_until(0) {}; Vote(order_id_t inOrderId, std::vector<driver_id_t> inPermittedDrivers, int inSeconds); Vote(const Vote & other) : m_order_id(other.m