stdmap

How to retrieve all keys (or values) from a std::map and put them into a vector?

做~自己de王妃 提交于 2019-11-26 03:47:42
问题 This is one of the possible ways I come out: struct RetrieveKey { template <typename T> typename T::first_type operator()(T keyValuePair) const { return keyValuePair.first; } }; map<int, int> m; vector<int> keys; // Retrieve all keys transform(m.begin(), m.end(), back_inserter(keys), RetrieveKey()); // Dump all keys copy(keys.begin(), keys.end(), ostream_iterator<int>(cout, \"\\n\")); Of course, we can also retrieve all values from the map by defining another functor RetrieveValues . Is there

How can I use std::maps with user-defined types as key?

点点圈 提交于 2019-11-26 03:14:12
问题 I\'m wondering why I can\'t use STL maps with user-defined classes. When I compile the code below, I get the following cryptic error message. What does it mean? Also, why is it only happening with user-defined types? (Primitive types are okay when they are used as key.) C:\\MinGW\\bin..\\lib\\gcc\\mingw32\\3.4.5........\\include\\c++\\3.4.5\\bits\\stl_function.h||In member function `bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Class1]\':| C:\\MinGW\\bin..\\lib\

In STL maps, is it better to use map::insert than []?

白昼怎懂夜的黑 提交于 2019-11-26 03:01:29
问题 A while ago, I had a discussion with a colleague about how to insert values in STL maps. I preferred map[key] = value; because it feels natural and is clear to read whereas he preferred map.insert(std::make_pair(key, value)) I just asked him and neither of us can remember the reason why insert is better, but I am sure it was not just a style preference rather there was a technical reason such as efficiency. The SGI STL reference simply says \"Strictly speaking, this member function is

How can I create my own comparator for a map?

非 Y 不嫁゛ 提交于 2019-11-26 02:32:33
问题 typedef map<string, string> myMap; When inserting a new pair to myMap , it will use the key string to compare by its own string comparator. Is it possible to override that comparator? For example, I\'d like to compare the key string by its length, not by the alphabet. Or is there any other way to sort the map? 回答1: std::map takes up to four template type arguments, the third one being a comparator. E.g.: struct cmpByStringLength { bool operator()(const std::string& a, const std::string& b)

Using char* as a key in std::map

允我心安 提交于 2019-11-26 01:07:24
问题 I am trying to figure out why the following code is not working, and I am assuming it is an issue with using char* as the key type, however I am not sure how I can resolve it or why it is occuring. All of the other functions I use (in the HL2 SDK) use char* so using std::string is going to cause a lot of unnecessary complications. std::map<char*, int> g_PlayerNames; int PlayerManager::CreateFakePlayer() { FakePlayer *player = new FakePlayer(); int index = g_FakePlayers.AddToTail(player); bool

Initializing a static std::map<int, int> in C++

我的梦境 提交于 2019-11-26 00:35:17
问题 What is the right way of initializing a static map? Do we need a static function that will initialize it? 回答1: Using C++11: #include <map> using namespace std; map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}}; Using Boost.Assign: #include <map> #include "boost/assign.hpp" using namespace std; using namespace boost::assign; map<int, char> m = map_list_of (1, 'a') (3, 'b') (5, 'c') (7, 'd'); 回答2: Best way is to use a function: #include <map> using namespace std; map<int,int> create