rewrite access to collection to avoid “double” finding

前端 未结 4 1032
广开言路
广开言路 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:19

    How about using size() to realize if an element was inserted, like this:

    auto old_size = futOrders.size();
    LimitOrder& order = futOrders[orderId];
    if (old_size < futOrders.size()) {
        LimitOrder& newOrder = order;
            // work
    } else {
        LimitOrder& futOrder = order;
            // another work
    }
    

提交回复
热议问题