stdmap

How to modify key values in std::map container

霸气de小男生 提交于 2019-11-29 04:23:45
Given std::map<int,std::string> myMap; fillMyMapWithStuff(myMap); // modify key values - I need to add a constant value to each key for (std::map<int,std::string>::iterator mi=myMap.begin(); mi != myMap.end(); ++mi) { // ... } Whats a good way apply some re-indexing? Must I remove the old entry and add a new one with the new key and old value? Looks like you are better off building a new map and swapping it afterward. You'll have only n insert operations instead of n deletions and n insertions. Yes, you have to remove the old entry and add a new one with the new key. Keys are not modifiable.

C++ const std::map reference fails to compile

不打扰是莪最后的温柔 提交于 2019-11-29 03:24:42
Is there a reason why passing a reference to a std::map as const causes the [] operator to break? I get this compiler error (gcc 4.2) when I use const: error: no match for ‘operator[]’ in ‘map[name]’ Here's the function prototype: void func(const char ch, std::string &str, const std::map<std::string, std::string> &map); And, I should mention that there is no problem when I remove the const keyword in front of std::map . If I've been instructed correctly, the [] operator will actually insert a new pair into the map if it doesn't find the key, which would of course explain why this happens, but

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

戏子无情 提交于 2019-11-29 00:19:37
问题 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

C++ std::map of template-class values

主宰稳场 提交于 2019-11-28 21:30:51
I'm attempting to declare a Row and a Column class, with the Row having a private std::map with values pointing to a templated Column . Something like this: template <typename T> class DataType { private: T type; }; template <typename T> class Field { private: T value; DataType<T> type; }; class Row { private: std::map<unsigned long,Field*> column; }; Well, I suppose in principle the Row class shouldn't have to know which kind of Field (or Column ) we'd like to use, i.e. whether it's a Field<int> in column 1 or a Field<double> in column 2. But I'm not sure what's the correct syntax for the Row

How can I delete elements of a std::map with an iterator?

≯℡__Kan透↙ 提交于 2019-11-28 17:09:47
I would like to loop through an std::map and delete items based on their contents. How best would this be done? If you have a C++11-compliant compiler, here's an easy way to do this: std::map<K, V>::iterator itr = myMap.begin(); while (itr != myMap.end()) { if (ShouldDelete(*itr)) { itr = myMap.erase(itr); } else { ++itr; } } The idea is to walk the iterator forward from the start of the container to the end, checking at each step whether the current key/value pair should be deleted. If so, we remove the element iterated over using the erase member function, which then returns an iterator to

How can I merge two STL maps?

旧街凉风 提交于 2019-11-28 16:57:00
问题 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. 回答1: 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>:

How to update std::map after using the find method?

荒凉一梦 提交于 2019-11-28 15:37:54
How to update the value of a key in std::map after using the find method? I have a map and iterator declaration like this: map <char, int> m1; map <char, int>::iterator m1_it; typedef pair <char, int> count_pair; I'm using the map to store the number of occurrences of a character. I'm using Visual C++ 2010. std::map::find returns an iterator to the found element (or to the end() if the element was not found). So long as the map is not const, you can modify the element pointed to by the iterator: std::map<char, int> m; m.insert(std::make_pair('c', 0)); // c is for cookie std::map<char, int>:

Recommended way to insert elements into map [duplicate]

这一生的挚爱 提交于 2019-11-28 15:00:28
问题 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:

std::map insert or std::map find?

梦想与她 提交于 2019-11-28 14:58:32
Assuming a map where you want to preserve existing entries. 20% of the time, the entry you are inserting is new data. Is there an advantage to doing std::map::find then std::map::insert using that returned iterator? Or is it quicker to attempt the insert and then act based on whether or not the iterator indicates the record was or was not inserted? The answer is you do neither. Instead you want to do something suggested by Item 24 of Effective STL by Scott Meyers : typedef map<int, int> MapType; // Your map type may vary, just change the typedef MapType mymap; // Add elements to map here int k

Are std::map and std::vector thread safe?

一个人想着一个人 提交于 2019-11-28 13:41:16
I am developing a multi threaded application, each thread will read (there will be no modifying of structures) from a group of maps and vectors. Can anyone please advise, since the threads are only reading from these structures would it be necessary to implement a sharable mutex around the code blocks where these structures are being read? alexrider In case of read only map/vector there is no need to use mutexes. This was already answered for both vector and map While C++03 doesn't mention threads, C++11 has clause covering you question. 23.2.2 Container data races [container.requirements