std::out_of_range异常
使⽤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::xx::at的使⽤)
std::out_of_range异常的描述
假设我们定义了⼀个map类型的变量g_mapIsDestroyedRefCount,要访问容器中的数据项有多种⽅式。例如,获取
g_mapIsDestroyedRefCount中key值为cameraId的值,可以这样:
1. g_mapIsDestroyedRefCount[cameraId]
2. g_mapIsDestroyedRefCount.at(cameraId)
两种写法都可以获取key为cameraId的value,⼀般效果看不出来差别,但是当g_mapIsDestroyedRefCount中不存在key为cameraId的<key, value>时就会出现“std::out_of_range”访问越界问题。
那么疑问来了:为什么at⽅式访问存在访问越界问题,[]⽅式访问不存在越界问题?因为C++容器类实现有关。
当[]⽅式访问不存在的<key, value>时为什么不会std::out_of_range
[]⽅式访问map容器元素时,遍历容器元素没到指定的键值对时会将相应的键值对插⼊,键为检索的key,value为零值。例如,下⾯例⼦中cameraId:2不在map中,这时通过[]式访问,会⾃动将<2, 0>插⼊到
g_mapIsDestroyedRefCount,然后返回[2]。
导致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  exception.  如果容器中没有匹配的k键,该函数将抛出⼀个out_of_range异常
如何避免std::out_of_range异常
正确使⽤
错误使⽤
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;
return0;
}
运⾏结果:
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;
}
运⾏结果:(程序异常退出)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。