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’ ...
随机推荐
- 加入强调语气,使用<strong>和<em>标签
有了段落又有了标题,现在如果想在一段话中特别强调某几个文字,这时候就可以用到<em>或<strong>标签. 但两者在强调的语气上有区别:<em> 表示强调,< ...
- php小知识点
1.字符串可以里面的字符可以像数组一样访问,比如$s = "123";$s[1]就等于2,如果字符串为中文则会乱码,需要使用mb_substr进行截取: 2.php中的单引号(' ...
- Linux导航神器-----autojump
对于命令行用户来说,频繁的cd和tab应该是日常工作中最多使用的命令了.特别对于重度用户来说,如果可以省去这么多cd和tab,将更多的时间做有意义的事该多好.其实Linux的学习过程本身就行这样.你会 ...
- 浮点与整形在GUI下的相关思考
平时不接触绘图,似乎感觉不到浮点和整形所带来的区别,这次项目中意外的碰到了浮点与整形进行迁移的工作.因此写点心得,让自己以后也可以看看. 用浮点作图有个最大的弊端就是边界情况,比如你需要在点(20,2 ...
- 织梦安装过后出现"...www/include/templets/default/index.htm Not Found!"
在织梦网站搬家之后再整站更新,往往会遇到访问首页的时候出现www/include/templets/default/index.htm Not Found!,这个问题我遇到过两次,都是这样解决的: 进 ...
- Python学习笔记:07异常
异常 Python用异常对象(Exception Object)来表示异常情况,当异常未被捕获时,就会产生回溯(Traceback) 异常分类 內建异常类:Exception,AttributeErr ...
- Day1 初识Python
(1)变量与赋值 name = "wanghuafeng" age = 29 print(name, age) a和b交换值 a = 3 b = 5 tmp = a a = b b ...
- kafka+storm连接
本项目为maven项目,需要添加必要的storm库,以及kafka依赖,使用storm自带的storm-kafka进行连接,根据自己集群环境 <dependency> <groupI ...
- MD3200扩展柜MD1200,玩起
杀到最后一关了,希望能让IOFENCE,IMPI之类的概念弄好. 至于ISCSI,CLVM,NAS,NFS,SAN这些东东的性能,我总觉得不如DAS,HBA卡直接到共享存储快.其它方案,不都灵活一点, ...
- 桌面小部件----LED电子时钟实现
桌面控件是通过 Broadcast 的形式来进行控制的,因此每个桌面控件都对应于一个BroadcastReceiver.为了简化桌面控件的开发,Android 系统提供了一个 AppWidgetPro ...