C++ vector下标越界的异常捕获

return归来 2017-12-01 10:18:12
#include <iostream>
#include <string>
#include <vector>
#include<stdexcept>
using namespace std;

int main()
{
try
{
vector<int> v{ 1,2,3,4 };
for (int i{ 0 }; i <= v.size(); ++i)
cout << v[i] << " ";
cout << endl;
}
catch (out_of_range)
{
cout << endl;
cerr << "out of range" <<endl;
}
catch (...)
{
cout << endl;
cerr << "something went wrong" << endl;
}
getchar();
}

这个是我尝试对下标越界的异常捕获处理程序,但在vc2017中运行时,总也捕获不了,如下图

但是把v[i]改成v.at(i)就可以了
如下图


还望大家指教!多谢
...全文
1312 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
八至 2021-05-21
  • 打赏
  • 举报
回复
vector用[]下标访问不会抛出异常,只有vec.at()取访问的时候才会抛出异常
赵4老师 2017-12-03
  • 打赏
  • 举报
回复
“多一少一”问题占程序员常犯错误的10%以上! 避免“多一少一”问题的方法之一是将比如<10甚至<5的数代入程序片断,掰手指头心算验证一下程序到底应该写为 x、x-1、x+1中的哪个? <、<=、==、>、>=中的哪个?
yshuise 2017-12-02
  • 打赏
  • 举报
回复
for (int i{ 0 }; i <= v.size(); ++i) ============================ 改为: i<v.size;
paschen 版主 2017-12-01
  • 打赏
  • 举报
回复
这里并没有抛出异,VC下DEBUG的实现只是一个断言失败,RELEASE则根本不去检查,是一个未定义行为,如果运气好,你的程序不会崩溃,只是多输出一个无意义的数字
jiht594 2017-12-01
  • 打赏
  • 举报
回复
Exception safety
If the container size is greater than n, the function never throws exceptions (no-throw guarantee).
Otherwise, the behavior is undefined.
jiht594 2017-12-01
  • 打赏
  • 举报
回复
public member function
<vector> std::vector::operator[] reference operator[] (size_type n);
const_reference operator[] (size_type n) const;Access element
Returns a reference to the element at position n in the vector container.

A similar member function, vector::at, has the same behavior as this operator function, except that vector::at is bound-checked and signals if the requested position is out of range by throwing an out_of_range exception.


Parameters
n
Position of an element in the container.
Notice that the first element has a position of 0 (not 1).
Member type size_type is an unsigned integral type.


Return value
The element at the specified position in the vector.

If the vector object is const-qualified, the function returns a const_reference. Otherwise, it returns a reference.

Member types reference and const_reference are the reference types to the elements of the container (see vector member types).


Example
1234567891011121314151617181920212223242526272829 // vector::operator[]
#include <iostream>
#include <vector>

int main ()
{
std::vector<int> myvector (10); // 10 zero-initialized elements

std::vector<int>::size_type sz = myvector.size();

// assign some values:
for (unsigned i=0; i<sz; i++) myvector[i]=i;

// reverse vector using operator[]:
for (unsigned i=0; i<sz/2; i++)
{
int temp;
temp = myvector[sz-1-i];
myvector[sz-1-i]=myvector[i];
myvector[i]=temp;
}

std::cout << "myvector contains:";
for (unsigned i=0; i<sz; i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';

return 0;
}



Output:

myvector contains: 9 8 7 6 5 4 3 2 1 0




Complexity
Constant.


Iterator validity
No changes.


Data races
The container is accessed (neither the const nor the non-const versions modify the container).
The reference returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.


Exception safety
If the container size is greater than n, the function never throws exceptions (no-throw guarantee).
Otherwise, the behavior is undefined.

注意最后2句
hongwenjun 2017-12-01
  • 打赏
  • 举报
回复
for (int i{ 0 }; i != v.size(); ++i)
区间操作,语法这样写,使用 !=
赵4老师 2017-12-01
  • 打赏
  • 举报
回复
其实电脑开机后物理内存的每个字节中都有值且都是可读写的,从来不会因为所谓的new、delete或malloc、free而被创建、销毁。区别仅在于操作系统内存管理模块在你读写时是否能发现并是否采取相应动作而已。操作系统管理内存的粒度不是字节而是页,一页通常为4KB。
赵4老师 2017-12-01
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧