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)就可以了
如下图


还望大家指教!多谢
...全文
1300 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即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
【为什么还需要学习C++?】 你是否接触很多语言,但从来没有了解过编程语言的本质?你是否想成为一名资深开发人员,想开发别人做不了的高性能程序?你是否经常想要窥探大型企业级开发工程的思路,但苦于没有基础只能望洋兴叹? 那么C++就是你个人能力提升,职业之路进阶的不二之选。【课程特色】 1.课程共19大章节,239课时内容,涵盖数据结构、函数、类、指针、标准库全部知识体系。2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。【学完后我将达到什么水平?】 1.对C++的各个知识能够熟练配置、开发、部署;2.吊打一切关于C++的笔试面试题;3.面向物联网的“嵌入式”和面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。【面向人群】 1.希望一站式快速入门的C++初学者; 2.希望快速学习 C++、掌握编程要义、修炼内功的开发者; 3.有志于挑战更高级的开发项目,成为资深开发的工程师。 【课程设计】 本课程包含3大模块基础篇本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。进阶篇本篇主要讲解编程中常用的一些技能,包含类的高级技术、类的继承、编译链接和命名空间等。提升篇:本篇可以帮助学员更加高效的进行c++开发,其中包含类型转换、文件操作、异常处理、代码重用等内容。

64,647

社区成员

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

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