今天就写一写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的一些异常的更多相关文章

  1. 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)

    string.vector 互转 string 转 vector vector  vcBuf;string        stBuf("Hello DaMao!!!");----- ...

  2. 实战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 ...

  3. 实战c++中的vector系列--vector&lt;unique_ptr&lt;&gt;&gt;初始化(全部权转移)

    C++11为我们提供了智能指针,给我们带来了非常多便利的地方. 那么假设把unique_ptr作为vector容器的元素呢? 形式如出一辙:vector<unique_ptr<int> ...

  4. 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])

    遍历一个vector容器有非常多种方法.使用起来也是仁者见仁. 通过索引遍历: for (i = 0; i<v.size(); i++) { cout << v[i] << ...

  5. 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())

    在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ...

  6. 实战c++中的string系列--不要使用memset初始化string(一定别这么干)

    參考链接: http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html 百度百科第一次这么给力: void *memset(voi ...

  7. 实战c++中的string系列--std::string与MFC中CString的转换

    搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ...

  8. 实战c++中的string系列--十六进制的字符串转为十六进制的整型(一般是颜色代码使用)

    非常久没有写关于string的博客了.由于写的差点儿相同了.可是近期又与string打交道,于是荷尔蒙上脑,小蝌蚪躁动. 在程序中,假设用到了颜色代码,一般都是十六进制的,即hex. 可是server ...

  9. 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)

    今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...

随机推荐

  1. bzero - 向字符串写入零

    总览 (SYNOPSIS) #include <string.h> void bzero(void *s, size_t n); 描述 (DESCRIPTION) bzero() 函数 把 ...

  2. 输入一个字符串输出ASCII的十六进制值

    #include <stdio.h> #include <string.h> #define LEN 1024 void main() { char s[LEN] = &quo ...

  3. 前端工程化与webpack

    (1) 前端工程化   近几年来,前端领域飞速发展,前端的工作早已不再是切几张图,写几个页面那么简单,项目比较大时,很可能会多人协同开发,模块化,组件化,CSS预编译等技术也被广泛的使用.前端自动化( ...

  4. git 提交运用vim编辑器

    git  commit -m 默认使用nano,觉得不爽,改成vim吧.在 .gitconfig (在根目录下)的  [core] 段中加上 editor=vim . 或:$git config -- ...

  5. vue中滚动页面,改变样式&&导航栏滚动时,样式透明度修改

    vue中滚动页面,改变样式&&导航栏滚动时,样式透明度修改.vue <div class="commonHeader" v-bind:class=" ...

  6. 【Hadoop】一、分布式数据库HBase简介

    1.分布式数据库特点   说到数据库,我们最熟悉的是类似于mysql这样的关系型数据库,称为RDBMS.关系型数据库作为一种数据存储和数据检索的关键技术,它支持SQL语言的结构化查询,但是它天生不是为 ...

  7. NET使用SuperSocket完成TCP/IP通信

    1)为什么使用SuperSocket? 性能高,易上手.有中文文档,我们可以有更多的时间用在业务逻辑上,SuperSocket有效的利用自己的协议解决粘包 2)SuperSocket的协议内容? 命令 ...

  8. js中sync、defer、async的区别

    <script src="script.js"></script> 没有 defer 或 async,浏览器会默认为同步sync,会立即加载并执行指定的脚本 ...

  9. 在CentOS6.4上安装GitLab

    1.Install and configure the necessary dependencies On CentOS 6 (and RedHat/Oracle/Scientific Linux 6 ...

  10. 树莓派 - 通过sysfs操控GPIO

    点亮或熄灭LED 硬件上,一个LED灯接在pi的Pin-25.  该引脚为BCM的GPIO26 $ gpio readall +-----+-----+---------+------+---+--- ...