stdmap

Why Can't I store references in a `std::map` in C++?

蹲街弑〆低调 提交于 2019-11-27 12:35:56
I understand that references are not pointers, but an alias to an object. However, I still don't understand what exactly this means to me as a programmer, i.e. what are references under the hood? I think the best way to understand this would be to understand why it is I can't store a reference in a map. I know I need to stop thinking of references as syntactic suger over pointers, just not sure how to :/ They way I understand it, references are implemented as pointers under the hood. The reason why you can't store them in a map is purely semantic; you have to initialize a reference when it's

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

丶灬走出姿态 提交于 2019-11-27 10:14:27
问题 I would like to loop through an std::map and delete items based on their contents. How best would this be done? 回答1: 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

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

≯℡__Kan透↙ 提交于 2019-11-27 09:18:51
问题 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. 回答1: 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

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

。_饼干妹妹 提交于 2019-11-27 08:56:51
问题 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? 回答1: 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>

C++ Thread-Safe Map

左心房为你撑大大i 提交于 2019-11-27 08:24:24
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. 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 map. There are some details, but everything is nicely documented and can give you an idea of the "concurrent

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

孤街醉人 提交于 2019-11-27 07:49:29
问题 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? 回答1: 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

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

a 夏天 提交于 2019-11-27 07:05:52
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::map while(it != internalMap.end()) { found = (it->second == value); if(found) break; ++it; } return

Preferred/idiomatic way to insert into a map

余生长醉 提交于 2019-11-27 06:02:49
I have identified four different ways of inserting 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?) icecrime First of all, operator[] and insert member functions are not functionally equivalent : The operator[] will search for the key, insert a default constructed value if not found, and return a reference to which you assign a value. Obviously

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

岁酱吖の 提交于 2019-11-27 04:31:30
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 from the map using the following code: std::string name; //here N is the field id for which I want the

How can I implement a map with different data types as values?

旧时模样 提交于 2019-11-27 03:32:09
问题 I want to put two (not more) different data types as values into a map as shown in the following example: typeX A, B, ...; typeY Z, Y, ...; void func (typeX) { ... } void func (typeY) { ... } std::map <std::string, what_to_put_here??> map; map["a"] = A; map["z"] = Z; ... std::vector<std::string> list; // This list will be something like "a", "y", ... for (unsigned int i = 0; i < list.size(); ++i) func( map[list[i]] ) Obviously this doesn't work, as the map will only accept one data type of