Returning an empty vector of strings if key is not found

前端 未结 4 808
花落未央
花落未央 2021-01-13 09:34

I know it is a very bad idea, so other suggestions on how to do it efficiently will be well-received.

Here\'s the thing. I have map

4条回答
  •  感情败类
    2021-01-13 10:07

    Most of the time you don't need a copy of the vector to be returned, a const reference will do just as well. You can always make a copy later if you need to.

    Also you don't need to iterate through a map, they're optimized for searching with the find method.

    const vector & returnEdges(string key)
    {
        map >::iterator it = outgoing.find(key);
        if (it == outgoing.end())
        {
            static vector empty_vector;
            return empty_vector;
        }
        return it->second;
    }
    

提交回复
热议问题