实战c++中的vector系列--vector的一些异常
今天就写一写vector的一些异常。能够捕捉的异常。
out_of_range
相当于数组的越界了。vector会自己主动增大容量,可是假设索引超出了当前的size。就会引发异常。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int>v(4);
std::cout << v[0] << std::endl;
std::cout << v[1] << std::endl;
std::cout << v[2] << std::endl;
std::cout << v[3] << std::endl;
std::cout << v[4] << std::endl;//越界
return 0;
}
除了使用索引外,还有使用vector.at()时发生越界:
#include <iostream> // std::cerr
#include <stdexcept> // std::out_of_range
#include <vector> // std::vector
int main (void) {
std::vector<int> myvector(10);
try {
myvector.at(20)=100; // vector::at throws an out-of-range
}
catch (const std::out_of_range& oor) {
std::cerr << "Out of Range error: " << oor.what() << '\n';
}
return 0;
}
std::length_error
在使用vector的时候,非常少会引发std::length_error异常,可是假设疏忽大意写这种代码:
#include <iostream> // std::cerr
#include <stdexcept> // std::length_error
#include <vector> // std::vector
int main (void) {
try {
// vector throws a length_error if resized above max_size
std::vector<int> myvector;
myvector.resize(myvector.max_size()+1);
}
catch (const std::length_error& le) {
std::cerr << "Length error: " << le.what() << '\n';
}
return 0;
}
vector* pData;
实战c++中的vector系列--vector的一些异常的更多相关文章
- 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)
string.vector 互转 string 转 vector vector vcBuf;string stBuf("Hello DaMao!!!");----- ...
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...
- 实战c++中的vector系列--vector<unique_ptr<>>初始化(全部权转移)
C++11为我们提供了智能指针,给我们带来了非常多便利的地方. 那么假设把unique_ptr作为vector容器的元素呢? 形式如出一辙:vector<unique_ptr<int> ...
- 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])
遍历一个vector容器有非常多种方法.使用起来也是仁者见仁. 通过索引遍历: for (i = 0; i<v.size(); i++) { cout << v[i] << ...
- 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())
在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ...
- 实战c++中的string系列--不要使用memset初始化string(一定别这么干)
參考链接: http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html 百度百科第一次这么给力: void *memset(voi ...
- 实战c++中的string系列--std::string与MFC中CString的转换
搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ...
- 实战c++中的string系列--十六进制的字符串转为十六进制的整型(一般是颜色代码使用)
非常久没有写关于string的博客了.由于写的差点儿相同了.可是近期又与string打交道,于是荷尔蒙上脑,小蝌蚪躁动. 在程序中,假设用到了颜色代码,一般都是十六进制的,即hex. 可是server ...
- 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)
今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...
随机推荐
- flutter 实现圆角头像的2种方法
圆角头像在开发中应用太普遍了,我总结了2种实现方法,分享给大家 方法一: 使用Container组件的decoration可以实现 Container( width: 40, height: 40, ...
- ztree 样式修改 white-space: nowrap; 后 下划线要是跟上的话 宽度 width 要 auto 就自动更新了
width:auto; border-bottom:1px solid #ccc; height:30px; display: inline-block;white-space: nowrap;
- 卸载钩子 UnhookWindowsHookEx
The UnhookWindowsHookEx function removes a hook procedure installed in a hook chain by the SetWindow ...
- asp.net的请求管道事件
一.引言 上篇介绍了Http的请求过程提到了在请求进入到创建完 HttpApplication 对象后,将进入一系列的处理事件Event,那么这些事件具体都有哪些呢,这篇文章就来简要阐述一下. 二.正 ...
- HTML head meta标签详细
<!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...
- 18第一章 ASP.Net内建对象
第一章 ASP.Net内建对象 第一章 ASP.Net内建对象 ASP.Net为保持用户的数据和信息,内建了许多对象,包括Application.Response.Requ ...
- hdfs深入:10、hdfs的javaAPI操作
/** * 递归遍历hdfs中所有的文件路径 */ @Test public void getAllHdfsFilePath() throws URISyntaxException, IOExcept ...
- linux下设置python3.x为默认版本
rm /usr/bin/python ln -s /usr/local/bin/python3.x /usr/bin/python sybomlic 安装目录 系统目录
- LeetCode 121. Best Time to Buy and Sell Stock (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 【02】AJAX XMLHttpRequest对象
AJAX XMLHttpRequest对象 XMLHttpRequest 对象用于与服务器交换数据,能够在不重新加载整个网页(刷新)的情况下,对网页进行部分更新. XMLHttpRequest 对 ...