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’ ...
随机推荐
- JVM内存管理及垃圾回收
一.JVM内存的构 Java虚拟机会将内存分为几个不同的管理区,这些区域各自有各自的用途,根据不同的特点,承担不同的任务以及在垃圾回收时运用不同的算法.总体分为下面几个部分: 程序计数器(Progra ...
- Activiti 工作流得到最后一次批注的时间
我们有时候在工作流开发中可能会遇到这样的需求,就是已经审批结束的流程,可能我们还是仍然需要修改业务表的结果,而且我们需要一个时间期限,比如:在5天内可以进行修改 ,这个时候我们就需要得到我们最后一步审 ...
- js学习笔记之:时间(一)
日期和时间是javaScript中常用的对象,可以通过此对象判断星期.生日.纪念日等,提高网站的人性化.下面将通过实例来介绍一下学习javaScript中有关时间和日期的知识点: (1)日期和时间函数 ...
- ul ol 列表的样式的控制
ul( Unordered List)无序列表 ol(Ordered List)有序列表 列表的样式: 列表原有符号.自定义图形符号.符号显示位置. 1.列表符号 是显示于每一个列表项目前的符号标识. ...
- PHP图形计算器(计算三角形矩形周长面积)
运用PHP面向对象的知识设计一个图形计算器,同时也运用到了抽象类知识,这个计算器可以计算三角形的周长和面积以及矩形的周长和面积.本图形计算器有4个页面:1.PHP图形计算器主页index.php; ...
- Day4 内置函数补充、装饰器
li = [11,22,33,44]def f1(arg): arg.append(55)#函数默认返回值None,函数参数传递的是引用li = f1(li) print(li) 内置函数补充: ...
- 死亡的协议--- Pieter Hintjens (ZeroMQ作者)
过去几年中用zeromq写过几个系统系统.对ZeroMQ强大和灵活印象非常深刻.在阅读zeromq guide文档时候.发现作者整理各种通信模式非常经典和实用,可以作为分布式通信的教科书来看.第一次见 ...
- PHP之路——MySql基础操作语句
1,创建数据库,create datebase 数据库名,在这里kenan建立一个数据库名为myfirstdb create database myfirstdb; 2,查看所有的数据库 show d ...
- Instruments 使用指南
Instruments 用户指南 http://cdn.cocimg.com/bbs/attachment/Fid_6/6_24457_90eabb4ed5b3863.pdf 原著:Apple Inc ...
- java实现DES加密与解密,md5加密
很多时候要对秘要进行持久化加密,此时的加密采用md5.采用对称加密的时候就采用DES方法了 import java.io.IOException; import java.security.Messa ...