stdmap

C++ map<std::string> vs map<char *> performance (I know, “again?”)

无人久伴 提交于 2019-11-30 03:06:57
I was using a map with a std::string key and while everything was working fine I wasn't getting the performance I expected. I searched for places to optimize and improved things only a little and that's when a colleague said, "that string key is going to be slow." I read dozens of questions and they consistently say: "don't use a char * as a key" " std::string keys are never your bottleneck" "the performance difference between a char * and a std::string is a myth." I reluctantly tried a char * key and there was a difference, a big difference. I boiled the problem down to a simple example:

Partial match for the key of a std::map

寵の児 提交于 2019-11-30 03:04:30
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("Marlon", "C")); return 0; } Now, I want to search for the position that holds the substring "Marl" and not

How can I display the content of a map on the console?

↘锁芯ラ 提交于 2019-11-30 02:51:54
I have a map declared as follows: map < string , list < string > > mapex ; list< string > li; How can I display the items stored in the above map on the console? Well it depends on how you want to display them, but you can always iterate them easily: typedef map<string, list<string>>::const_iterator MapIterator; for (MapIterator iter = mapex.begin(); iter != mapex.end(); iter++) { cout << "Key: " << iter->first << endl << "Values:" << endl; typedef list<string>::const_iterator ListIterator; for (ListIterator list_iter = iter->second.begin(); list_iter != iter->second.end(); list_iter++) cout <

C++: Inheriting from std::map

纵然是瞬间 提交于 2019-11-30 00:26:39
问题 I want to inherit from std::map , but as far as I know std::map hasn't any virtual destructor. Is it therefore possible to call std::map 's destructor explicitly in my destructor to ensure proper object destruction? 回答1: The destructor does get called, even if it's not virtual, but that's not the issue. You get undefined behavior if you attempt to delete an object of your type through a pointer to a std::map . Use composition instead of inheritance, std containers are not meant to be

Last key in a std::map

被刻印的时光 ゝ 提交于 2019-11-30 00:06:51
问题 I am looking for the highest key value (a defined by the comparison operator) of a std::map. Is this guaranteed to be map.rbegin()->first ? (I am a bit shaky on reverse iterators, and how much freedom there is in the implementation of std::map) If not, please advise. I cannot change the data structure. 回答1: Yes. Map is a sorted container, the reverse iterator must return the elements in reverse (i.e. decreasing) order of their keys. [Edit: as Charles Bailey points out in his answer, your code

How can I merge two STL maps?

拈花ヽ惹草 提交于 2019-11-29 21:12:41
How can I merge two STL maps into one? They both have the same key and value types ( map<string, string> ). If there is an overlap of the keys, I would like to give preference to one of the maps. jkerian Assuming you want to preserve the elements in mapA , and merge elements in mapB for which there is no key in mapA : mapA.insert(mapB.begin(), mapB.end()) will do what you want, I think. Working example: #include <iostream> #include <map> void printIt(std::map<int,int> m) { for(std::map<int,int>::iterator it=m.begin();it!=m.end();++it) std::cout << it->first<<":"<<it->second<<" "; std::cout <<

Recommended way to insert elements into map [duplicate]

此生再无相见时 提交于 2019-11-29 19:30:02
Possible Duplicate: In STL maps, is it better to use map::insert than []? I was wondering, when I insert element into map, what is the recommended way. Should I map[key] = value; or map.insert(std::pair<key_type, value_type>(key, value)); I did the following quick test: #include <map> #include <string> #include <iostream> class Food { public: Food(const std::string& name) : name(name) { std::cout << "constructor with string parameter" << std::endl; } Food(const Food& f) : name(f.name) { std::cout << "copy" << std::endl; } Food& operator=(const Food& f) { name = f.name; std::cout << "=" << std:

C++ std::map items in descending order of keys

左心房为你撑大大i 提交于 2019-11-29 16:19:41
问题 How cal I use std::map container with key value in descending order. As an example, if insert the following items: [2 , 5] [1 , 34] [3 , 67] They will be ordered in the map like: position 0: [1, 34] position 1: [2, 5] position 2: [3, 67] I can iterate through the map reversely, but suppose the next time I am inserting [-1 , 60]. Will it be placed at the first position? 回答1: Use a custom comparator when the default order doesn't do it for you. You pass it as the third template parameter ( that

Is is possible to use std::map in C++ with a class without any copy operator?

自闭症网瘾萝莉.ら 提交于 2019-11-29 06:02:44
I'm using a Class (Object) that doesn't have any copy operator : it basically cannot be copied right now. I have a std::map<int,Object> objects variable that lists objects with an int identifier. How could I add an Object to this map without having to use copy operators? I tried objects.insert(std::pair<0,Object()>); but that won't compile. I would just like to create my object initially inside the map using the default constructor, but writing objects[0]; fails... Thanks :) In C++03, objects that are stored in STL containers must be copyable. This is because a STL container's std::allocator

Why use std::less as the default functor to compare keys in std::map and std::set?

≡放荡痞女 提交于 2019-11-29 05:25:12
I am wondering why std::map and std::set use std::less as the default functor to compare keys. Why not use a functor that works similar to strcmp? Something like: template <typename T> struct compare { // Return less than 0 if lhs < rhs // Return 0 if lhs == rhs // Return greater than 0 if lhs > rhs int operator()(T const& lhs, T const& rhs) { return (lhs-rhs); } } Say a map has two object in it, with keys key1 and key2 . Now we want to insert another object with key key3 . When using std::less , the insert function needs to first call std::less::operator() with key1 and key3 . Assume std: