stdmap

std::string as a key in std::map using a compare operator

元气小坏坏 提交于 2019-12-06 00:23:42
I'm trying to use a std::string as a key in a std::map however, i'm unable to find() correctly. My code is somewhat complicated and large so this is a small program that demonstrates the problem I'm having. If someone could tell me why this doesn't work, i'd be very grateful. Thanks. #include <stdio.h> #include <string> #include <map> struct comparer { public: bool operator()(const std::string x, const std::string y) { return x.compare(y)==0; } }; int main(int argc, char *argv[]) { std::map<std::string, int, comparer> numbers; numbers.insert(std::pair<std::string,int>("One",1)); numbers.insert

How to use struct as key in std::map

[亡魂溺海] 提交于 2019-12-05 08:15:43
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: //.h #include <map> using namespace std; map<GUID,GUID> mapGUID; //.cpp GUID tempObj1, tempObj2;

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

这一生的挚爱 提交于 2019-12-05 07:43:31
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_order_id), m_time_until(other.m_order_id) { m_drivers_responses = other.m_drivers_responses; m_permitted

Copy std::map to std::set in c++

此生再无相见时 提交于 2019-12-05 06:19:23
Is it possible with a STL algorithm to deep copy a std::map values to a std::set? I don't want to explicitly insert in the new set. I don't want to explicitly do this: std::map<int, double*> myMap; //filled with something std::set<double*> mySet; for (std::map<int, double*>::iterator iter = myMap.begin(); iter!=myMap.end(); ++iter) { mySet.insert(iter->second); } but find a more coincise and elegant way to do this, with a deep copy of values. What about this? std::transform(myMap.begin(), myMap.end(), std::inserter(mySet, mySet.begin()), [](const std::pair<int, double*>& key_value) { return

Remove element from std::map based on the time of insertion

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 01:45:52
I need to erase elements from an std::map based on the time of insertion (or something else more efficient than that). The map will probably hold thousands of elements and if I store the time and iterate the map to check each elements time, it will probably end up being quite time consuming. Does anyone have any good idea how to erase elements from a std::map when they are getting old? The std::map<> type has no notion of when an element was inserted. It only serves to hold a key / value pair mapping. It also has no notion of insert order so it can't even provide a relative type of insert. To

How can I use a struct as key in a std::map?

青春壹個敷衍的年華 提交于 2019-12-04 16:25:29
问题 I have the following code, but I get an error on on the last line: struct coord { int x, y; bool operator=(const coord &o) { return x == o.x && y == o.y; } bool operator<(const coord &o) { return x < o.x || (x == o.x && y < o.y); } }; map<coord, int> m; pair<coord, int> p((coord{0,0}),123); m.insert(p); // ERROR here How can I use a struct as key in a map? I tried to change the code to this: struct coord { int x, y; bool const operator==(const coord &o) { return x == o.x && y == o.y; } bool

Why does std::unordered_map have a reserve method?

☆樱花仙子☆ 提交于 2019-12-04 16:15:31
问题 According to this you cannot reserve space for std::map : No, the members of the map are internally stored in a tree structure. There is no way to build the tree until you know the keys and values that are to be stored. From this it is obvious why std::map would lack a reserve() method, which it does on cppreference.com. However, std::unordered_map does have a reserve() method, but when I try to use it with operator[] , insert() or emplace() they all go to allocate memory despite me having

use of emplace(args&& …) in associative containers

天涯浪子 提交于 2019-12-04 12:16:10
I am trying to forward some arguments to do inplace construction of objects . I don't quite get the rationale behind the usage of emplace in associative containers or may be I am just using/thinking in a wrong way. It would be great if someone can share code snippets for usage. Associative container like map always store an object of kind pair() , and the emplace function says that it will call the constructor of the object stored (which for is always pair in case of maps) by forwarding the arguments. So are we just restricted to provide two arguments (key , value) even if the the function has

Use of for_each on map elements

↘锁芯ラ 提交于 2019-12-04 07:43:08
问题 I have a map where I'd like to perform a call on every data type object member function. I yet know how to do this on any sequence but, is it possible to do it on an associative container? The closest answer I could find was this: Boost.Bind to access std::map elements in std::for_each. But I cannot use boost in my project so, is there an STL alternative that I'm missing to boost::bind? If not possible, I thought on creating a temporary sequence for pointers to the data objects and then, call

how are map iterators invalidated when erasing elements? [duplicate]

笑着哭i 提交于 2019-12-04 06:40:36
This question already has an answer here: Iterator invalidation rules 5 answers when and how are iterators invalidated in a map when using the erase method ? for example : std :: map < int , int > aMap ; aMap [ 33 ] = 1 ; aMap [ 42 ] = 10000 ; aMap [ 69 ] = 100 ; aMap [ 666 ] = -1 ; std :: map < int , int > :: iterator itEnd = aMap.lower_bound ( 50 ) ; for ( std :: map < int , int > :: iterator it = aMap.begin ( ) ; it != itEnd ; // no-op ) { aMap.erase ( it ++ ) ; } the erased iterator will surely become invalid (it's incremented while still valid) but what about the others? if I'm not wrong