-3

我很困扰。为什么会这样:

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

这失败了:

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));

这意味着,第一个变体以一个键/值对结束,而第二个变量保持映射不变(0 个条目)?

4

2 回答 2

1

为我工作:

#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;

}

并输出:

$g++ map_test.cpp 
$ ./a.out 
1.1
2.2
于 2013-06-17T20:03:32.133 回答
0

谢谢你们的帮助,伙计们。我现在觉得有点傻。地图为空的假设是基于调试器中的外观。我使用 XCode 作为 IDE,当使用指向地图的指针时,它只会搞砸并将地图显示为空。使用 cout 揭示了真相。

于 2013-06-18T10:01:24.510 回答