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的更多相关文章

  1. Python Tutorial 学习(八)--Errors and Exceptions

    Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...

  2. Handling Errors and Exceptions

    http://delphi.about.com/od/objectpascalide/a/errorexception.htm Unfortunately, building applications ...

  3. 《The Python Tutorial》——Errors and Exceptions 阅读笔记

    Errors and Exceptions 官方文档:https://docs.python.org/3.5/tutorial/errors.html python中所有的异常都继承自BaseExce ...

  4. 笔记-python-tutorial-8.errors and exceptions

    笔记-python-tutorial-8.errors and exceptions 1.      errors and exceptions 1.1.    syntax errors >& ...

  5. [译]The Python Tutorial#8. Errors and Exceptions

    [译]The Python Tutorial#Errors and Exceptions 到现在为止都没有过多介绍错误信息,但是已经在一些示例中使用过错误信息.Python至少有两种类型的错误:语法错 ...

  6. 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 ...

  7. python异常和错误(syntax errors 和 exceptions)

    语法错误 语法错误又被称解析错误 >>> for i in range(1..10):print(i) File "<stdin>", line 1 ...

  8. Python Errors and Exceptions

    1. python中的try{}catch{} 2. raise exception 3. try...except ... else.. 4. finally块 python中的异常处理的keywo ...

  9. ruby Errors & Exceptions

    When you first started coding, errors were probably the last thing you wanted to see. After all, it’ ...

随机推荐

  1. UITableView-FDTemplateLayoutCell自动计算UITableView高度的使用

    基本应用如果你有self-satisfied cell,那么你应该做的是:#import "UITableView+FDTemplateLayoutCell.h"- (CGFloa ...

  2. MySQL 管理

    MySQL 管理 启动及关闭 MySQL 服务器 首先,我们需要通过以下命令来检查MySQL服务器是否启动: ps -ef | grep mysqld 如果MySql已经启动,以上命令将输出mysql ...

  3. 对C#泛型实例化对像--转

    最近在编写一套开发框架结构主要应用.Net 3.5以上的框架开发与应用.在此框架中应用了较多的泛型.下面来讲讲对泛型的实例化,以代码为例,如: public class A { } public cl ...

  4. js ie浏览器下的选中操作

    最近在学习jquery  好多英文网站,制作一个网站的副本,可以主动地学习.好像给自己的网站添加一个小词典,就像沪江小d那样. js试了好几种方法 实在不行,网上搜索了下 ,用到了浏览器开发.本篇文章 ...

  5. angular-fullstack test

    1:运行yo 提示我可以升级到1.4.7版本,下面进行升级 提示需要npm>=2.8.0下面进行升级npm y@y:angular-fullstack-test$ npm install npm ...

  6. 启动weblogic11g一直提示<141281> <unable to get file lock, will retry ...>

    一次非正常关闭weblogic之后,再次启动时启动不成功,一直提示:<141281> <unable to get file lock, will retry ...> 解决方 ...

  7. Mahout

    http://blog.csdn.net/yueyedeai/article/details/26567379

  8. cf D. Maximum Submatrix 2

    http://codeforces.com/contest/376/problem/D 题意:给你一个矩阵,可以随意排列n行的次序,然后找出全部含有1的子矩阵.输出1的个数. 思路:c[i][j]表示 ...

  9. POJSorting It All Out (拓扑)

    题目链接. 题目大意: 给定一定的数量的小于关系: 1.如果发现环,输出从前几次就可以确定出现环 2.如果能够确定唯一序列,输出. 3.如果通过全部关系,还不能确定序列,则输出不能确定. 分析: 个人 ...

  10. COJ 0579 4020求次短路的长度

    4020求次短路的长度 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 在一个地图上共有N个路口(编号分别为1到N),R条道路( ...