标签:std map cameraId range key out
使用C++容器类访问成员时由于使用问题可能会遇到"terminate called after throwing an instance of 'std::out_of_range'"或者"Abort message: 'terminating with uncaught exception of type std::out_of_range"。问题的大概意思是:访问越界了。没有捕获std::out_of_range类型的异常终止。
通常在使用vector、map这样的C++容器类型时会遇到,这里我们以map类型为例,加以说明。
- std::out_of_range异常的描述
- 导致std::out_of_range的原因
- 如何避免std::out_of_range异常(即std::xx::at的使用)
std::out_of_range异常的描述
假设我们定义了一个map类型的变量g_mapIsDestroyedRefCount,要访问容器中的数据项有多种方式。例如,获取g_mapIsDestroyedRefCount中key值为cameraId的值,可以这样:
- g_mapIsDestroyedRefCount[cameraId]
- g_mapIsDestroyedRefCount.at(cameraId)
两种写法都可以获取key为cameraId的value,一般效果看不出来差别,但是当g_mapIsDestroyedRefCount中不存在key为cameraId的<key, value>时就会出现“std::out_of_range”访问越界问题。
导致std::out_of_range的原因
容器类型访问方法使用有问题
对于std::map::at官方声明:
mapped_type& at (const key_type& k); const mapped_type& at (const key_type& k) const;对于std::map::at使用有如下说明: Access element 访问元素
Returns a reference to the mapped value of the element identified with key k. 返回元素键为k的映射值的引用,即Key为k的元素的对应value值。
If k does not match the key of any element in the container, the function throws an out_of_range exception. 如果容器中没有匹配的k键,该函数将抛出一个out_of_range异常
std::map::at的使用
- 正确使用
- 错误使用
1.std::map::at的正确使用
#include <iostream> #include <string> #include <map> std::map<int, int> g_mapIsDestroyedRefCount; int main() { int cameraId = 1; cout << "Let's try"<< endl; //向map中添加测试数据 g_mapIsDestroyedRefCount.insert(std::pair<int, int>(0, 2))' cout << "cameraId:"<< cameraId<< "count:"; try { cout<< g_mapIsDestroyedRefCount.at(cameraId) <<endl; } catch (const std::out_of_range& oor) { std::cerr << "\nOut of range error:" << oor.what()<< endl; } cout << "try done"<< endl; return 0; }
运行结果:
2.std::map::at错误使用
#include <iostream> #include <string> #include <map> using namespace std; std::map<int, int> g_mapIsDestroyedRefCount; int main() { int cameraId = 2; cout<< "Let's try"<< endl; g_mapIsDestroyedRefCount.insert(std::pair<int, int>(0, 2)); cout<< "cameraId:"<< cameraId<< "count:"; //介绍中说的方法一,可以访问 cout<< g_mapIsDestroyedRefCount[cameraId]<< endl;
//方法二,异常
cameraId = 2; count<< g_mapIsDestroyedRefCount.at(cameraId)<< endl;
cout<< "try done"<< endl;
}
运行结果:(程序异常退出)
标签:std,map,cameraId,range,key,out 来源: https://www.cnblogs.com/yemaomao/p/12358166.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。