哪一个是更好的检查,如果关键是在hash_map在c++ ?At with try..catch块或find with迭
Which one is better to check if the key is in the hash_map in C++? at with try...catch block or find with iterator comparison?
据我所知,有两种基本方法可以检查条目是否在hash_map中:
假设我们有一个hash_map: hash_map<string, int> amap
如果我们要检查abc是否在映射中那么我们可以使用
hash_map<string, int>::iterator itr = amap.find("abc");
if (itr != amap.end()) //in the map
或:
try {
int value = amap.at("abc");
}
catch(out_of_range& e) {
//not there
}
想知道哪个更好吗?出于效率考虑?
使用find()
。测试迭代器几乎肯定比捕获异常要便宜得多。
相关文章:
- Problems with std::cin.fail()
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- "CID"在AT+HTTPPARA= "CID" ,1中是什么意思
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- Boost.TEST with CLion: "Test framework quit unexpectedly"
- 避免碎片化的ClientHellos with OpenSSL (DTLS)
- Issues with Win32 ReadProcessMemory API
- Qt with WinAPI MouseProc
- [[maybe_unused]] with structured_binding?
- Issue with WriteProcessMemory
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- OpenCV RTP-Stream with FFMPEG
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- CMake 用于设定C++标准的设施 "at least C++NN"
- std::adjacent_difference with std::chrono time_point
- SIGSEGV on Boost UDP 套接字关闭 - tcache_get at malloc.c.
- 使用 mat.at<uchar>(i,j) 的 opencv C++中的矩阵赋值错误
- CMake Error at modules/videoio/cmake/detect_ffmpeg.cmake:16
- 哪一个是更好的检查,如果关键是在hash_map在c++ ?At with try..catch块或find with迭