stdmap

Can I access a C++11 std::map entry while inserting/erasing from another thread?

安稳与你 提交于 2019-12-01 04:06:41
问题 Can I access (without locking) an std::map entry while another thread inserts/erases entrys? example pseudo C++: typedef struct { int value; int stuff; }some_type_t; std::map<char,some_type_t*> my_map; //thread 1 does: my_map.at('a')->value = 1; //thread 2 does: some_type_t* stuff = my_map.at('b'); //thread 3 does: my_map.erase('c'); //I'm not modifying any elements T is a pointer to an previously allocated "some_type_t" std C++11 says that all members should be thread safe (erase() is not

iterator vs reverse_iterator

蓝咒 提交于 2019-12-01 03:19:05
问题 I'm using std::map to store a lot of elements (pairs of elements) and I have a "little" doubt. What is more efficient to iterate all elements over my std::map , iterator or reverse_iterator ? 回答1: Does it really matter? these are the types of the micro optimizations you must try to avoid IMHO. Also, even if the iteration time changes for very large number of elements in the map, the fact that you are trying to iterate through all the elements of such a big map means that most probably you

std::map change key_comp after initialization

会有一股神秘感。 提交于 2019-12-01 02:24:25
问题 It is possible to change the comparison method of a std::map after it has been created and initialized? Or maybe only after it has been created?? I want to alter somehow the behavior of a class that contains a map that I cannot change the definition. I want to change it's comparison behavior maybe by passing another map. 回答1: Maybe it is possible, this is untested: Define your own custom comparator, which internally has a pointer to the real implementation of the comparison function Pass an

How to convert a sorted std::list of std::pair to a std::map

旧时模样 提交于 2019-12-01 01:54:49
问题 I have got a std::list< std::pair<std::string,double> > , which I know is sorted according to the std::string element . Since I would like to do a lot of std::find_if based on the std::string element, I believe a std::map<string,double,MyOwnBinaryPredicate> with lower_bound and upper_bound would be more adequate. The fact is that I want to insert elements in the std::map in an efficient way. So I want to use an additional iterator to make the insert faster. I believe the easiest way would be

Character Array as a value in C++ map

大憨熊 提交于 2019-11-30 20:27:37
I want to define something like Map<int, char[5] > myMap; The above declaration is accepted by c++ compiler and no error is thrown but when I do something like this int main() { char arr[5] ="sdf"; map <int, char[5]> myMap; myMap.insert(pair<int, char[5]>(0,arr)); return 0; } I get error as: In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0, from /usr/include/c++/4.6/bits/char_traits.h:41, from /usr/include/c++/4.6/ios:41, from /usr/include/c++/4.6/ostream:40, from /usr/include/c++/4.6/iostream:40, from charMap.cpp:1: /usr/include/c++/4.6/bits/stl_pair.h: In constructor ‘std:

Java equivalent of C++ std::map?

社会主义新天地 提交于 2019-11-30 19:33:57
I'm looking for a Java class with the characteristics of C++ std::map's usual implementation (as I understand it, a self-balancing binary search tree): O(log n) performance for insertion/removal/search Each element is composed of a unique key and a mapped value Keys follow a strict weak ordering I'm looking for implementations with open source or design documents; I'll probably end up rolling my own support for primitive keys/values. This question's style is similar to: Java equivalent of std::deque , whose answer was "ArrayDeque from Primitive Collections for Java". ConcurrentSkipListMap is a

C++: Inheriting from std::map

旧街凉风 提交于 2019-11-30 17:20:20
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? 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 inherited, and you shouldn't. I'm assuming you want to extend the functionality of std::map (say you want to find

Last key in a std::map

坚强是说给别人听的谎言 提交于 2019-11-30 16:47:47
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. 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 gives the greatest key if it exists - i.e. if the map is non-empty] Yes, but remember to check that map

Character Array as a value in C++ map

こ雲淡風輕ζ 提交于 2019-11-30 05:03:05
问题 I want to define something like Map<int, char[5] > myMap; The above declaration is accepted by c++ compiler and no error is thrown but when I do something like this int main() { char arr[5] ="sdf"; map <int, char[5]> myMap; myMap.insert(pair<int, char[5]>(0,arr)); return 0; } I get error as: In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0, from /usr/include/c++/4.6/bits/char_traits.h:41, from /usr/include/c++/4.6/ios:41, from /usr/include/c++/4.6/ostream:40, from /usr

Java equivalent of C++ std::map?

◇◆丶佛笑我妖孽 提交于 2019-11-30 03:31:07
问题 I'm looking for a Java class with the characteristics of C++ std::map's usual implementation (as I understand it, a self-balancing binary search tree): O(log n) performance for insertion/removal/search Each element is composed of a unique key and a mapped value Keys follow a strict weak ordering I'm looking for implementations with open source or design documents; I'll probably end up rolling my own support for primitive keys/values. This question's style is similar to: Java equivalent of std