FAQ Database Discussion Community


C++: strange behaviour with the pointers to std::map

c++,pointers,gcc,stdmap
I need to share some variable (myMap) with the inner class. I tried to do it using following approach: class SecondClass { public: std::map< std::string, short > *myMapPtr; }; class TestClass { public: SecondClass secondClass; std::map< std::string, short > myMap; TestClass() { printf( "consturctor of TextClass\n" ); secondClass.myMapPtr = &myMap;...

Braced initialisation of std::map member compiler error

c++,c++11,stdmap,brace-initialization
The following code does not compile using Visual Studio 2013. It does compile using Xcode 6.1 (Clang 3.5). std::string s1("one"); std::string s2("two"); std::string s3("three"); std::string s4("four"); class X { typedef std::map<std::string, std::string> MyMapType; MyMapType map1 = { { s1, s2 }, { s3, s4 } }; MyMapType map2 = {...

Java hashmap.get() in c++

c++,stdmap
In java you could have something like: Map<Foo, List<Bar>> things; for(Foo foo : things.getKeySet()){ List<bar> = things.get(foo); } Is there an equivalent for c++, maybe in std::map? Thanks for any help....

How to find out under which key of a map a given value is stored?

c++,string,c++11,stdvector,stdmap
std::map<std::string, std::vector<std::string>> myMap How can we find out under which key of the myMap the string '55' is stored? std::vector<string> vec1 = {"11", "22", "33"}; std::vector<string> vec2 = {"44", "55"}; std::vector<string> vec3 = {}; std::string key1 = "1"; std::string key2 = "2"; std::string key3 = "3"; myMap.insert(std::make_pair(key1, vec1)); myMap.insert(std::make_pair(key2, vec2));...

std::map Members refering all to one address

c++,std,stdmap
I am using std::map, similar to the implemtentation in webrtc (see here). My Map is defined like that: typedef std::map <std::string, BaseOption*> OptionMap; And the BaseOption (Option) is defined: class BaseOption { public: virtual ~BaseOption() {} }; template<typename T> class Option : public BaseOption { public: explicit Option(T* v) {value...

Which vector and map, uses less memory (large set of data and unknown size)

c++,stdvector,large-data,stdmap
I wonder which container uses less memory between std::map and std::vector with a large set of data. Loads of posts talk about efficiency, and my priority is not efficiency but memory consumption. So, if we don't know the number of our data (in my case can be over 12,000,000 entries,...

C ++ std::map with custom comparer for keeping tournament of matches

c++,stdmap
I would like to save results of tournament in some container. For every match I need to store names of players and a number of points. For example: map["player1:player2"] = {2,4}; I want to retrieve from this container not only by key "player1:player2", but even by the inverse key "player2:player1"...

Adding element into C++ std::map inserts only one key

c++,stdmap
I created a simple library program, where I store a map of Book objects and its quantity. I would like to add some books to the map to be further able to rent a book, etc. The problem is that in my code, method called createLib inserts only one book...

std::map with string key and custom value field

c++,visual-c++,stdmap
How to create a std::map with string key and custom value field. I want a std::map as below: "Name" "abcd" "age" "50" "Address" "Street" "xxxx" "PIN" "xxxx" "District" "xxxx" "Gender" "Male" So my all my fields except the third one are string,string type. But the address is itself an another...

C++ map with pointer as key. Memory management

c++,pointers,boost,stdmap
I've an STL like implementation of a map with pointer as keys. As I noticed the keys won't get freed by the map. Because I cannot keep references to all pointers I can not manage the memory myself. I read something about smart pointers raising two questions: Is there another...

transform algorithm gives “binary '=' no operator which takes left-hand operand..” [duplicate]

c++,assignment-operator,stdmap
This question already has an answer here: How to apply transform to an STL map in C++ 1 answer pair<CDrug, pair<unsigned,double>> expirednull(pair<CDrug, pair<unsigned,double>> temp){ if (temp.first.isValid() == false) temp.second.first = 0; return temp; } string checkForExpiredDrugs() { stringstream s; vector<CDealer>::iterator it1; map<CDrug, pair<unsigned, double>> d; map<CDrug, pair<unsigned, double>>::iterator it2;...

find element with max value from std::map [duplicate]

c++,std,stdmap
This question already has an answer here: C++ Help finding the max value in a map 6 answers I'm trying to get the element with max value from std::map, int main() { map<int, int> m; m[1] = 100; m[2] = -1; auto x = std::max_element(m.begin(), m.end(), m.value_comp()); cout <<...

Cast between two slightly different std::map

c++,casting,stl,callback,stdmap
I have a std::map defined as follows: typedef void (SomeClass::*callback_f)(); typedef std::pair<std::string, callback_f> my_pair; typedef std::map<char,my_pair> my_map1; There are multiple of these definitions, so there is my_map1, my_map2, etc. and every one of them differs from the other for the class SomeClass, which is SomeClass, SomeClassTwo, etc. My need...

c++ map array operator overloading enable setting value to

c++,operator-overloading,stdmap
i try to do ordered map ( simple very simple ) i want to overload the map "[] = value " operator . that is the equal sign that come after the array operator but how ? i have this : template <typename K, class V> class OrderedMap { public:...

SIGSEGV with std::map of pointers to template objects

c++,templates,inheritance,g++,stdmap
I need to keep a std::map of pointers to templated objects. To get rid of templates, I use a common untemplated base class. When running the code, I get a SIGSEGV signal. Debugging shows that the problem arises with statement data_[id] = s; It could be a problem related to...

openMp : parallelize std::map iteration

c++,openmp,stdmap
There are some posts about this issue but none of them satisfies me. I don't have openMp 3.0 support and I need to parallelize a iteration over a map. I want to know if this solution would work or not : auto element = myMap.begin(); #pragma omp parallel for shared(element)...

c++ find a key match in a bi-dimensional map (map of map)

c++,stdmap
I have the following map that I use to store customer names and a dictionary of itens selected with its code and description, like: Name Item Code, Item Description Customer1 -> 1234, Item 1 1233, Item 2 1232, Item 3 So I´m using the following structure: std::map<std::string, std::map<std::string, std::string>> purchase_list;...

std::make_map? uniform initialization

c++,initializer-list,stdmap,uniform-initialization,stdtuple
The following works: std::map<int,int> m{{1,2}}; std::tuple<std::map<int,int>,int> tm1{m, 2}; std::tuple<std::map<int,int>,int> tm2{{{1,2}}, 2}; std::tuple<std::map<int,int>,int> tmm1 = std::make_tuple(m, 2); The following does not: std::tuple<std::map<int,int>,int> tmm2 = std::make_tuple({{1,2}}, 2); Is there a way to initialize a map inside a tuple made with make_tuple without using an extra variable?...

C++: Using type as map key introduces template substitution errors

c++,c++11,variadic-templates,stdmap,stdtuple
I'm using a tuple as a key to track elements in a map, and then later iterating over the map to produce a string version of the map. To help me with the conversion, I have a template convenience function that will concatenate the tuple used as the key (inspired...

how to check a map is null or not in c++?

c++,segmentation-fault,stdmap
I am trying to check if a map is empty or not in c++. However, I get a segmentation fault, although the map is not NULL. The code is so simple and here: void func(std::map<std::string, std::vector<Foo*> >* myMap){ if(myMap == NULL) std::cout << "My map is Null" << std::endl; else...

Why does std::map reinstantiate it's value when it's created through operator[]

c++,pointers,visual-studio-2005,hierarchy,stdmap
I implement a class that stores a number of Parent objects inside an std::map. Each Parent has a Child. That Child has a pointer to it's Parent, which is set in Parent's constructor. It goes like this: I call std::map::operator[], it calls Parent's constructor, which sets child.parent to this, and...

How to use emplace() in a std::map whose value is a std::set (map from something to a set)?

c++,c++11,stdmap,stdset,emplace
The Problem I have a std::map<int, std::set<int>> named misi. I'm wondering why misi.emplace(2345, {6, 9}); and misi.emplace({2345, {6, 9}}); don't work as expected, as shown below. The Code #include <set> // std:set #include <map> // std::map #include <utility> // std::piecewise_construct, std::pair #include <tuple> // std::forward_as_tuple #include <iostream> // std::cout, std::endl...