rewrite access to collection to avoid “double” finding

前端 未结 4 1087
广开言路
广开言路 2021-01-25 04:43

I have such code:

std::unordered_map futOrders;

auto i = futOrders.find(orderId);
if (i == futOrders.end()) {
    LimitOrd         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-25 05:00

    Assuming there is a way to "determine if an order is empty", you could do:

    LimitOrder& anOrder = futOrders[orderId];
    
    if (anOrder.empty())       
    {
       // New order, do stuff that only new orders need. 
    }
    else
    {
       // Old order, update it.
    }
    

    The empty method could of course be something like if (anOrder.name == "") or if (anOrder.orderId == 0), etc.

提交回复
热议问题