using pair and map

Dear all,

I'm trying to create a map reference which should containt a pair as a key and a bool vector as the value i.e. such a thing

 
map<pair<int,int> * ,vector<bool>* >* linkWaveMapping = new map<pair<int,int>*, vector<bool>*>;


It seems I cannot correctly make a pair with some int's :

1
2
3
int srcAddr= 1;
int destAddr= 2;
pair<int,int>* thePair = new pair(srcAddr,destAddr);


yields the following

Description Resource Path Location Type cannot convert 'int*' to 'std::pair<int, int>*' in initialization PCE_CHILD.cc /geysers_simulator/src/Network	line 784	C/C++ Problem

and

Description Resource Path Location Type
expected ',' or ';' before 'pair' PCE_CHILD.cc	/geysers_simulator/src/Network	line 784 C/C++ Problem

and

Description	Resource	Path	Location	Type
expected type-specifier before 'pair'	PCE_CHILD.cc	/geysers_simulator/src/Network	line 784	C/C++ Problem


Also I cannot add to the map (bitmapToSend is a bool vector created correctly):

 
linkWaveMapping->insert(new pair< pair<int,int>*,vector<bool>* (thePair,bitmapToSend));


with output
Description	Resource	Path	Location	Type
Invalid arguments '
Candidates are:
std::pair<std::_Rb_tree_iterator<std::pair<std::pair<int,int> * const,std::vector<bool,std::allocator<bool>> *>>,bool> insert(const std::pair<std::pair<int,int> * const,std::vector<bool,std::allocator<bool>> *> &)
std::_Rb_tree_iterator<std::pair<std::pair<int,int> * const,std::vector<bool,std::allocator<bool>> *>> insert(std::_Rb_tree_iterator<std::pair<std::pair<int,int> * const,std::vector<bool,std::allocator<bool>> *>>, const std::pair<std::pair<int,int> * const,std::vector<bool,std::allocator<bool>> *> &)
void insert(#10000, #10000)
'	PCE_CHILD.cc	/geysers_simulator/src/Network	line 866	Semantic Error


Can somebody help me witht his?

Kind regards

Jens Buysse
There is no type deduction with template types so you always need to specify the types.
pair<int,int>* thePair = new pair<int,int>(srcAddr,destAddr);

It looks like you use a lot of pointers everywhere. Is this really necessary? The code usually gets much more clean if you try to avoid pointers when not necessary.
Last edited on
Hello,

indeed I saw the solution of the first problem while review the code for the second time. Thanks for this.

To answer your second question: I do need these pointers, as I'm using the Omnet simulator and I need to exchange messages between objects with some information and that information is to be stored in pointers.

The second question still remains unsolved for me.
insert takes as argument a pair (not a pointer to a pair) with the key as first and value type as second element.
I think this is correct but as you can see, it gets quite unreadable.
linkWaveMapping->insert(pair<pair<int,int>*,vector<bool>* >(thePair,bitmapToSend));

To make it a bit more readable you can use the function make_pair.
linkWaveMapping->insert(make_pair(thePair,bitmapToSend));

I still think you are overusing pointers by the way. If you didn't know, you can get references/pointers to objects not allocated with new. There is rarely a good reason to allocate containers with new as you do with the map.
Last edited on
The insert doesn't want a pointer to a pair it wants a pair. This compiles for me. Does this help?
http://www.cplusplus.com/reference/stl/map/insert/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <map>
#include <vector>
using namespace std;

int main(){
    map<pair<int,int> * ,vector<bool>* >* linkWaveMapping = new map<pair<int,int>*, vector<bool>*>;

    int srcAddr= 1;
    int destAddr= 2;
    pair<int,int>* thePair = new pair<int,int>(srcAddr,destAddr);
    vector<bool>* bitmapToSend;

   linkWaveMapping->insert( pair< pair<int,int>*,vector<bool>* > (thePair,bitmapToSend));

   return 0;
}

Also you might want to make this:
 
linkWaveMapping->insert( pair< pair<int,int>*,vector<bool>* > (thePair,bitmapToSend));

be
 
linkWaveMapping->insert( make_pair(thePair,bitmapToSend));

to read easier.


Sorry for the double post, to slow!


Last edited on
Thanks for the advice everybody, but it seems the solutions you are providing don't work in my code...

I found a solution but I don't know why this works while the other not:

Working code :
 
(*linkWaveMapping)[thePair] = bitmapToSend;


Not working code
 
linkWaveMapping->insert( pair< pair<int,int>*,vector<bool>* > (thePair,bitmapToSend));
What does "Not working code" mean? Not compile or logic?

There is a difference between insert and the operator[]. Insert will not insert the value if it is there already and operator[] will over write the value with the new one.

"Because map containers do not allow for duplicate key values, the insertion operation checks for each element inserted whether another element exists already in the container with the same key value, if so, the element is not inserted and its mapped value is not changed in any way."
With work I mean "not compiling". So I cannot test the logic of the two approaches as one of them does not compile.
Post the compile error and code.
Last edited on
It are the same errors as in the original question...
I solved the issue indeed as Peter87 suggested by limiting the Pointer usage. I needed a pointer for the linkWaveMapping but as turns out by inserting key-value pairs copies of these keys-pairs are made and are stored for the life time of the container's life. So I did not need to have the key value pairs as pointers.

But still, strange my code did not work..
Topic archived. No new replies allowed.