stdmap

std map composite key

巧了我就是萌 提交于 2019-12-23 21:13:10
问题 I have a problem with the operator<() method which is required for a std::map. I'm using a struct as composite key that looks as follows: struct MyKey { std::string string1; std::string string2; std::string string3; unsigned int uint1; friend bool operator<(const MyKey& mk1, const MyKey& mk2) { return mk1.string1 < mk2.string1 && mk1.string2 < mk2.string2 && mk1.string3 < mk2.string3 && mk1.uint1 < mk2.uint1; } } As introduced I want to use a composite key with 4 values, but I don't know how

Moving keys out of std::map<> &&

a 夏天 提交于 2019-12-23 15:22:27
问题 I'd like to have let's say getKeys() function getting not-copiable keys out of a map : class MyObj { // ... complex, abstract class... }; struct Comparator { bool operator()(std::unique_ptr<MyObj> const &a, std::unique_ptr<MyObj> const &b); }; std::vector<std::unique_ptr<MyObj>> getKeys(std::map<std::unique_ptr<MyObj>, int, Comparator> &&map) { std::vector<std::unique_ptr<MyObj>> res; for (auto &it : map) { res.push_back(std::move(it.first)); } return res; } But it is not working because the

c++ value_type not work for std::tr1:tuple in a std::map

丶灬走出姿态 提交于 2019-12-23 12:16:31
问题 The following code snippet work with Visual Studio 2008 but not with Visual Studio 2010. template <typename TKey> struct MyStruct { typedef std::map<TKey, int> Keys; MyStruct() { } void set(TKey& key) { #if 1 // This works with VS 2008 but not with 2010 keys_.insert(typename Keys::value_type(key, 1)); #else // This works with VS 2008 and VS 2010 keys_.insert(std::pair<TKey, int>(key, 1)); #endif }; private: Keys keys_; }; Usage typedef std::tr1::tuple<int, int> MyValueType; MyStruct

C++ Storing references to values in std::map

旧街凉风 提交于 2019-12-23 08:44:07
问题 Am I right in assuming that adding/removing elements to an std::map does not effect the other elements (ie cause them to be relocated in memory) and so that the following is safe: I looked at various sites with info on the container but only found out about the cases where iterators are invalidated, which I already know... std::map<std::string,std::string> map; PopulateMap(map); std::string &a= map["x"]; AddMoreData(map); RemoveRandomKeysExceptX(map); map["x"] = "foo"; std::cout << a << " " <

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

风格不统一 提交于 2019-12-21 18:02:15
问题 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

C++ Long switch statement or look up with a map?

﹥>﹥吖頭↗ 提交于 2019-12-21 06:47:23
问题 In my C++ application, I have some values that act as codes to represent other values. To translate the codes, I've been debating between using a switch statement or an stl map. The switch would look something like this: int code; int value; switch(code) { case 1: value = 10; break; case 2: value = 15; break; } The map would be an stl::map<int, int> and translation would be a simple lookup with the code used as the key value. Which one is better/more efficient/cleaner/accepted? Why? 回答1:

Most efficient way to assign values to maps

旧巷老猫 提交于 2019-12-21 03:44:21
问题 Which way to assign values to a map is most efficient? Or are they all optimized to the same code (on most modern compilers)? // 1) Assignment using array index notation Foo["Bar"] = 12345; // 2) Assignment using member function insert() and STL pair Foo.insert(std::pair<string,int>("Bar", 12345)); // 3) Assignment using member function insert() and "value_type()" Foo.insert(map<string,int>::value_type("Bar", 12345)); // 4) Assignment using member function insert() and "make_pair()" Foo

Copy std::map into std::vector of pairs

五迷三道 提交于 2019-12-19 12:28:09
问题 I'm trying to copy a map into a vector of pair, so I can then sort the vector by the second data member of the pairs. I have resolved this doing like this: void mappedWordsListSorter(){ for (auto itr = mappedWordsList.begin(); itr != mappedWordsList.end(); ++itr){ vectorWordsList.push_back(*itr); } sort(vectorWordsList.begin(), vectorWordsList.end(), [=](pair<string, int>& a, pair<string, int>& b){return a.second > b.second;}); } I need to find a way to do this without using a raw loop, using

C++ std::map or std::set - efficiently insert duplicates

一个人想着一个人 提交于 2019-12-18 16:11:25
问题 I have a bunch of data full of duplicates and I want to eliminate the duplicates. You know, e.g. [1, 1, 3, 5, 5, 5, 7] becomes [1, 3, 5, 7]. It looks like I can use either std::map or std::set to handle this. However I'm not sure whether it's faster to (a) simply insert all the values into the container, or (b) check whether they already exist in the container and only insert if they don't - are inserts very efficient? Even if there's a better way... can you suggest a fast way to do this?

Partial match for the key of a std::map

匆匆过客 提交于 2019-12-18 11:28:09
问题 I have an std::map and I want to search for a key using a substring. For example, I have the following code: #include <iostream> #include <map> #include <string> using namespace std; typedef std::map<std::string, std::string> TStrStrMap; typedef std::pair<std::string, std::string> TStrStrPair; int main(int argc, char *argv[]) { TStrStrMap tMap; tMap.insert(TStrStrPair("John", "AA")); tMap.insert(TStrStrPair("Mary", "BBB")); tMap.insert(TStrStrPair("Mother", "A")); tMap.insert(TStrStrPair(