-3

I am very confused. Why does this work:

double doubleValue = 20;
NcVar variable = {some process obtaining an instance}
map<NcVar,double> th;
th.insert(std::make_pair(variable, doubleValue));

and this fails:

double doubleValue = 20;
NcVar variable = {some process obtaining an instance}
map<NcVar,double> *th = new map<NcVar,double>();
th->insert(std::make_pair(variable, doubleValue));

That means, the first variant ends up with one key/value-pair, while the second leaves the map unchanged (0 entries)?

Jürgen Simon
  • 876
  • 1
  • 12
  • 35
  • 5
    How do you know that it fails? There's nothing wrong with the code you've posted here, so I assume the error is elsewhere. – templatetypedef Jun 17 '13 at 19:10
  • 3
    You will get better help if you post a complete program and describe what behavior you expect and what behavior you observe. – Johan Råde Jun 17 '13 at 19:13
  • I know it fails because in the first case the map has an entry. In the second code it winds up empty. I do check this in the debugger. The rest of the code is really of no consequence, just parsing some command line parameters. Nothing spectacular. – Jürgen Simon Jun 17 '13 at 19:17
  • Yes, it is std::map. I am using boost (among other things), but that should not interfere with std::map afaik. – Jürgen Simon Jun 17 '13 at 19:18
  • 2
    If you are so sure it fails, post some simple code that shows this. It is hard to see how it could be possible. – juanchopanza Jun 17 '13 at 19:21
  • 3
    'Works for me' http://ideone.com/3P1mUA – Mike Vine Jun 17 '13 at 19:24
  • Does not work for me. I extirpated the code from the larger program it was embedded in into a standalone. Same trouble. See here: http://ideone.com/XiAetN PS: you will not be able to run this without a NetCDF-File that is CF-Metadata compliant. This is why I was trying to avoid posting it here. – Jürgen Simon Jun 17 '13 at 19:59
  • What fails? run-time erro? – digit plumber Jun 17 '13 at 20:10

2 Answers2

1

Works for me:

#include <map>
#include <iostream>
using namespace std;
int main(){
  typedef map<int,float> mapp;
  mapp map1;
  map1.insert(make_pair(1,1.1));

  mapp * mp2 = new mapp();
  mp2->insert(make_pair(2,2.2));
  cout << map1.begin()->second << endl;
  cout << mp2->begin()->second <<endl;
return 0;

}

And output:

$g++ map_test.cpp 
$ ./a.out 
1.1
2.2
digit plumber
  • 1,140
  • 2
  • 14
  • 27
0

Thanks for the help, guys. I feel kinda stupid now. The assumption that the map was empty was based on the appearance in the debugger. I am using XCode as IDE and when using a pointer to map, it would simply mess up and display the map as empty. Using cout revealed the truth.

Jürgen Simon
  • 876
  • 1
  • 12
  • 35