Newb question here: 如何將存儲在Maptest [2]中的值與變量一起更新? 我想你可以用指針做到這一點,但是這並不工作:在C++中更新地圖值
map<int, int*> MapTest; //create a map
int x = 7;
//this part gives an error:
//"Indirection requires pointer operand ("int" invalid)"
MapTest[2] = *x;
cout << MapTest[2]<<endl; //should print out 7...
x = 10;
cout <<MapTest[2]<<endl; //should print out 10...
我在做什麼錯?
旁註:該'&'取地址,''是一個引用操作和derefs一個指針。 – birryree 2011-12-31 19:34:18
既然你想要指向x的指針,你應該這樣做,而不是:'MapTest [2] =&x;' – lfxgroove 2011-12-31 19:36:45
代碼在幾個方面是錯誤的。應該是MapTest [2] =&x; '並使用'* MapTest [2]'來訪問值。你是否熟悉指針? – 2011-12-31 19:39:41