C++ Question

本帖最後由 luckiejacky 於 2014-9-29 09:00 編輯

請問如何在window.at(position)
沒有這個數值後傳回NULL

The intention of the code is to first check if the window (there exists some agents within the window),
if the "position" key is found, I just need to add the unit to the current "frame", otherwise, create a new window
THX
  1. std::map<int, std::vector<ObjectBase*>> window;

  2. void Coordinater::putUnitInWindow(ObjectBase *unit, int position)
  3. {
  4.                                          
  5.         std::vector<ObjectBase*> unitsAtT = window.at(position);
  6.                                          
  7.         // if not found in nodepool
  8.         if (unitsAtT.empty())
  9.         {
  10.                 unitsAtT.push_back(unit);                                                 
  11.                 window.insert(std::make_pair(position, unitsAtT));
  12.                                          
  13.         }
  14.         else {
  15.                 unitsAtT.push_back(unit);                                                 
  16.         }
  17.                        
  18. }
  19.                                
複製代碼
THX

好耐未掂過C++...

===========================

std::map::at

Returns a reference to the mapped value of the element with key equivalent to key. If no such element exists, an exception of type std::out_of_range is thrown.

即係CATCH EXCEPTION 便可以了...
唔想用EXCEPTION, 可以用FIND來代替,

TOP

相關文章