stdmap

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

心已入冬 提交于 2019-11-28 11:29:02
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 value. However, when looping over list , the call to func() should be unambiguous since the type of map

std::map thread-safety

烈酒焚心 提交于 2019-11-28 06:52:52
Is reference to object in std::map is thread safe? std::map< std::string, Object > _objects; map can be changed from many threads and this access is synchronized, but reference to value (Object &) accessable just from 1 instance and thread. is write operations with Object & is safe if another thread will add items to map? will it reallocate? The C++11 standard guarantees that const method access to containers is safe from different threads (ie, both use const methods). In addition, [container.requirements.dataraces] states implementations are required to avoid data races when the contents of

How can I use a std::map with std::weak_ptr as key?

▼魔方 西西 提交于 2019-11-28 03:01:18
问题 How can I use std::weak_ptr as key for a std::map as shown in the following code? #include <map> #include <memory> int main() { std::map< std::weak_ptr<int>, bool > myMap; std::shared_ptr<int> sharedptr(new int(5)); std::weak_ptr<int> weakptr = sharedptr; myMap[weakptr] = true; return 0; } The above program doesn't build and trying to compile it gives many error messages such as: 1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std:

Is there a Java Map keySet() equivalent for C++'s std::map?

北城以北 提交于 2019-11-27 23:46:50
Is there a Java Map keySet() equivalent for C++'s std::map ? The Java keySet() method returns "a set view of the keys contained in this map." All of the answers presented thus far end up creating a std::set directly, which may not be ideal: if you only want to be able to iterate over the keys, you don't want to have the overhead of creating a whole new container. A more flexible option would be to use a transform iterator that converts a std::map iterator into some type of iterator that just yields the key when dereferenced. This is rather straightforward using the Boost Transform Iterator:

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

人盡茶涼 提交于 2019-11-27 22:56:08
问题 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:

How can I find the minimum value in a map?

六眼飞鱼酱① 提交于 2019-11-27 22:02:51
I have a map and I want to find the minimum value (right-hand side) in the map. Here is how I did it: bool compare(std::pair<std::string ,int> i, pair<std::string, int> j) { return i.second < j.second; } //////////////////////////////////////////////////// std::map<std::string, int> mymap; mymap["key1"] = 50; mymap["key2"] = 20; mymap["key3"] = 100; std::pair<char, int> min = *min_element(mymap.begin(), mymap.end(), compare); std::cout << "min " << min.second<< " " << std::endl; The code above works fine and I'm able to get the minimum value. However, when I put this code inside my class as

Custom types as key for a map - C++

倖福魔咒の 提交于 2019-11-27 20:31:05
I am trying to assign a custom type as a key for std::map . Here is the type which I am using as key. struct Foo { Foo(std::string s) : foo_value(s){} bool operator<(const Foo& foo1) { return foo_value < foo1.foo_value; } bool operator>(const Foo& foo1) { return foo_value > foo1.foo_value; } std::string foo_value; }; When used with std::map , I am getting the following error. error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Foo' (or there is no acceptable conversion) c:\program files\microsoft visual studio 8\vc\include\functional 143 If I change the

What is difference between const and non const key?

一笑奈何 提交于 2019-11-27 20:25:55
问题 What is the difference between the following two lines? map<int, float> map_data; map<const int, float> map_data; 回答1: int and const int are two distinct types. std::map<int, float> and std::map<const int, float> are, similarly, different types. The difference between std::map<const int, float> and std::map<int, float> is, to a degree, analogous to the difference between, say, std::map<int, float> and std::map<std::string, float> ; you get a fresh map type for each. In the non- const case,

Find mapped value of map

两盒软妹~` 提交于 2019-11-27 20:00:50
Is there a way in C++ to search for the mapped value (instead of the key) of a map, and then return the key? Usually, I do someMap.find(someKey)->second to get the value, but here I want to do the opposite and obtain the key (the values and keys are all unique). Because of how a map is designed, you'll need to do the equivalent of a search on unordered data. for (it = someMap.begin(); it != someMap.end(); ++it ) if (it->second == someValue) return it->first; Pavan Chandaka Using lambdas (C++11 and newer) //A MAP OBEJCT std::map<int, int> mapObject; //INSERT VALUES mapObject.insert(make_pair(1,

Intersection of two `std::map`s

守給你的承諾、 提交于 2019-11-27 14:43:37
Given that I have two std::map s, say: map<int, double> A; map<int, double> B; I'd like to get the intersection of the two maps, something of the form: map<int, pair<double,double> > C; Where the keys are the values in both A and B and the value is a pair of the values from A and B respectively. Is there a clean way using the standard-library? Mark Ransom template<typename KeyType, typename LeftValue, typename RightValue> map<KeyType, pair<LeftValue, RightValue> > IntersectMaps(const map<KeyType, LeftValue> & left, const map<KeyType, RightValue> & right) { map<KeyType, pair<LeftValue,