STL之Errors and Exceptions
Error Handling
STL设计的目标是性能最优化,而不是最安全。
错误检查是极其浪费时间的,因此,STL对于错误处理几乎没有做处理,因此,这对STL的使用者的要求就非常高。
为什么不采取错误处理呢,下面是两个主要原因:
- Error checking reduces performance, and speed is still a general goal of programs. As mentioned, good performance was one of the design goals of the STL.
- If you prefer safety over speed, you can still get it, either by adding wrappers or by using special versions of the STL. But you can't program to avoid error checking to get better performance when error checking is built into all basic operations. For example, when every subscript operation checks whether a range is valid, you can't write your own subscripts without checking. However, it is possible the other way around.
Pay Attention
要不出错地使用STL,下面几点是必须满足的:
Iterators must be valid. For example, they must be initialized before they are used. Note that iterators may become invalid as a side effect of other operations. In particular, they become invalid for vectors and deques if elements are inserted or deleted, or reallocation takes place.
Iterators that refer to the past-the-end position(结束之后的位置) have no element to which to refer. Thus, calling operator * or operator -> is not allowed. This is especially true for the return values of the end() and rend() container member functions.
Ranges must be valid:
Both iterators that specify a range must refer to the same container.
The second iterator must be reachable from the first iterator.
If more than one source range is used, the second and later ranges must have at least as many elements as the first one.
Destination ranges must have enough elements that can be overwritten; otherwise, insert iterators must be used.
possible errors
// stl/iterbug1.cpp #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> coll1; //empty collection
vector<int> coll2; //empty collection /* RUNTIME ERROR:
* - beginning is behind the end of the range
*/
vector<int>::iterator pos = coll1.begin();
reverse (++pos, coll1 .end()); //insert elements from 1 to 9 into coll2
for (int i=; i<=; ++i) {
coll2.push_back (i);
} /*RUNTIME ERROR:
* - overwriting nonexisting elements
*/
copy (coll2.begin(), coll2.end(), //source
coll1 .begin()) ; //destination /* RUNTIME ERROR:
* - collections mistaken
* - begin() and end() mistaken
*/
copy (coll1.begin(), coll2.end(), //source
coll1. end()); //destination
}
Note that these errors occur at runtime, not at compile time, and thus they cause undefined behavior.
STL之Errors and Exceptions的更多相关文章
- Python Tutorial 学习(八)--Errors and Exceptions
Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...
- Handling Errors and Exceptions
http://delphi.about.com/od/objectpascalide/a/errorexception.htm Unfortunately, building applications ...
- 《The Python Tutorial》——Errors and Exceptions 阅读笔记
Errors and Exceptions 官方文档:https://docs.python.org/3.5/tutorial/errors.html python中所有的异常都继承自BaseExce ...
- 笔记-python-tutorial-8.errors and exceptions
笔记-python-tutorial-8.errors and exceptions 1. errors and exceptions 1.1. syntax errors >& ...
- [译]The Python Tutorial#8. Errors and Exceptions
[译]The Python Tutorial#Errors and Exceptions 到现在为止都没有过多介绍错误信息,但是已经在一些示例中使用过错误信息.Python至少有两种类型的错误:语法错 ...
- Laravel API Errors and Exceptions: How to Return Responses
Laravel API Errors and Exceptions: How to Return Responses February 13, 2019 API-based projects are ...
- python异常和错误(syntax errors 和 exceptions)
语法错误 语法错误又被称解析错误 >>> for i in range(1..10):print(i) File "<stdin>", line 1 ...
- Python Errors and Exceptions
1. python中的try{}catch{} 2. raise exception 3. try...except ... else.. 4. finally块 python中的异常处理的keywo ...
- ruby Errors & Exceptions
When you first started coding, errors were probably the last thing you wanted to see. After all, it’ ...
随机推荐
- idea sass scss配置
1.安装Ruby win 直接http://rj.baidu.com/soft/detail/22711.html?ald mac linux https://ruby.taobao.org/ 可下 ...
- Java基础--说集合框架
版权所有,转载注明出处. 1,Java中,集合是什么?为什么会出现? 根据数学的定义,集合是一个元素或多个元素的构成,即集合一个装有元素的容器. Java中已经有数组这一装有元素的容器,为什么还要新建 ...
- phpcms首页调用会员头像及金钱、积分等信息,并按照积分点数排列
<div class="box"> <h5>最新会员</h5> <div class="col-auto">&l ...
- excel设置单元格不可编辑
把允许编辑的单元格选定,右键-设置单元格格式-保护,把锁定前的对钩去掉.再点工具-保护工作表.这样就可以只让你刚才设定的单元格允许编辑,其他不允许.
- 网易DBA私享会分享会笔记2
mysql索引与查询优化什么是索引?索引其实是一个目录.通过各种数据结构实现,是(值=>行位置)的映射 索引的作用:1.提高访问速度2.实现主键.唯一键逻辑 索引使用场景数据量特别大的时候,进行 ...
- 编写可维护的javascript代码--- 2015.11.21(基本格式化)
1.1 每行的编码需要控制在80字符. 1.2 改用:的地方必须用上. 1.3 缩进用2个制表符,不过4个也可以. 1.4 当代码一行显示不全需要折行显示,这里我暂且假定缩进为4个字符. 1.5 如果 ...
- angularJS快速入门
1.引入脚本文件 <link rel="stylesheet" href="http://lib.sinaapp.com/js/bootstrap/v3.0.0/c ...
- VM虚拟机上 实现CentOS 6.X下部署LVS(DR)+keepalived实现高性能高可用负载均衡
一.简介 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一. ...
- PS:改装店收的是友情价,包安装十五个毛主席。
糟糠之妻下堂,娇俏公主上位--更换宝马三系座椅作业 - 切诺基 Jeep家族 越野e族论坛 越野/SUV/旅行/赛事/改装/互动中心 PS:改装店收的是友情价,包安装十五个毛主席.
- 使用 FreeMarker 替换 JSP 的 10 个理由
你还在使用 Java 服务器页面(俗称JSP)吗?我曾经也是,但是几年前我抛弃了它们,并且再也没有用过JSP了.JSP 是个很好的概念,但是它却剥夺了 web 开发的乐趣. 对我而言,这些都是小事,比 ...