Gri*_*lik 0 c++ stl stdmap c++20
c++中什么效率更高?
if (my_map.contains(my_key)) return my_map.at(my_key);
Run Code Online (Sandbox Code Playgroud)
或者
try { return my_map.at(my_key); } catch (std::out_of_range e) { ... }
Run Code Online (Sandbox Code Playgroud)
?
最有效的方法是使用std::map<>::find
:
if (auto it = my_map.find(my_key); it != my_map.end()) return *it;
Run Code Online (Sandbox Code Playgroud)
contains
这避免了对follow by 的双重查找以及对 if throwsat
的昂贵的异常处理at
归档时间: |
|
查看次数: |
72 次 |
最近记录: |