stdmap

Checking value exist in a std::map - C++

寵の児 提交于 2019-11-26 17:37:02
问题 I know find method finds the supplied key in std::map and return an iterator to the element. Is there anyway to find the value and get an iterator to the element? What I need to do is to check specified value exist in std::map. I have done this by looping all items in the map and comparing. But I wanted to know is there any better approach for this. Here is what I have wrote bool ContainsValue(Type_ value) { bool found = false; Map_::iterator it = internalMap.begin(); // internalMap is std:

C++ Thread-Safe Map

大兔子大兔子 提交于 2019-11-26 14:08:14
问题 Does anyone know where I can find an implimentation that wraps a std::map and makes it thread safe? When I say thread safe I mean that it offers only serial access to the map, one thread at a time. Optimally, this map should use only the standard-library and / or boost constructs. 回答1: Does not meet the criteria that you have specified, but you could have a look at the TBB containers. There is so called concurrent_hash_map which allows multiple threads to access concurrently the data in the

How can I create my own comparator for a map?

耗尽温柔 提交于 2019-11-26 12:54:00
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? 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) const { return a.length() < b.length(); } }; // ... std::map<std::string, std::string, cmpByStringLength> myMap

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

无人久伴 提交于 2019-11-26 12:36:28
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 unnecessary: it exists only for convenience." Can anybody tell me that reason, or am I just dreaming that

What is the preferred/idiomatic way to insert into a map?

丶灬走出姿态 提交于 2019-11-26 11:49:16
问题 I have identified four different ways of inserting elements into a std::map : std::map<int, int> function; function[0] = 42; function.insert(std::map<int, int>::value_type(0, 42)); function.insert(std::pair<int, int>(0, 42)); function.insert(std::make_pair(0, 42)); Which of those is the preferred/idiomatic way? (And is there another way I have not thought of?) 回答1: First of all, operator[] and insert member functions are not functionally equivalent : The operator[] will search for the key,

Thread safety of std::map for read-only operations

﹥>﹥吖頭↗ 提交于 2019-11-26 11:13:40
问题 I have a std::map that I use to map values (field ID\'s) to a human readable string. This map is initialised once when my program starts before any other threads are started, and after that it is never modified again. Right now, I give every thread its own copy of this (rather large) map but this is obviously inefficient use of memory and it slows program startup. So I was thinking of giving each thread a pointer to the map, but that raises a thread-safety issue. If all I\'m doing is reading

How can i estimate memory usage of std::map?

三世轮回 提交于 2019-11-26 09:34:51
问题 For example, I have a std::map with known sizeof(A) and sizeof(B), while map has N entries inside. How would you estimate its memory usage? I\'d say it\'s something like (sizeof(A) + sizeof(B)) * N * factor But what is the factor? Different formula maybe? Maybe it\'s easier to ask for upper bound? 回答1: The estimate would be closer to (sizeof(A) + sizeof(B) + ELEMENT_OVERHEAD) * N + CONTAINER_OVERHEAD There is an overhead for each element you add, and there is also a fixed overhead for

Why does std::map operator[] create an object if the key doesn&#39;t exist?

你说的曾经没有我的故事 提交于 2019-11-26 09:09:33
问题 I\'m pretty sure I already saw this question somewhere (comp.lang.c++? Google doesn\'t seem to find it there either) but a quick search here doesn\'t seem to find it so here it is: Why does the std::map operator[] create an object if the key doesn\'t exist? I don\'t know but for me this seems counter-intuitive if you compare to most other operator[] (like std::vector) where if you use it you must be sure that the index exists. I\'m wondering what\'s the rationale for implementing this

std::map default value

荒凉一梦 提交于 2019-11-26 08:56:05
问题 Is there a way to specify the default value std::map \'s operator[] returns when an key does not exist? 回答1: No, there isn't. The simplest solution is to write your own free template function to do this. Something like: #include <string> #include <map> using namespace std; template <typename K, typename V> V GetWithDef(const std::map <K,V> & m, const K & key, const V & defval ) { typename std::map<K,V>::const_iterator it = m.find( key ); if ( it == m.end() ) { return defval; } else { return

std::map default value for build-in type

梦想与她 提交于 2019-11-26 08:25:25
问题 Recently, I was confused by the std::map operator[] function. In the MSDN library, it says: \"If the argument key value is not found, then it is inserted along with the default value of the data type.\" I tryed to search much more exactly explanation for this issue. For example here: std::map default value In this page, Michael Anderson said that \"the default value is constructed by the default constructor(zero parameter constructor)\". Now my quest comes to this:\"what the default value for