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. UNIX基础知识

    一.线程 线程是进程某程序段的一次运行. 1.线程共享资源,利用共享的资源,线程很容易能够互相通信 (1)进程代码段:每个线程有各自的寄存器组,在运行时期拷贝给cpu寄存器,来确定运行的是哪段代码段. ...

  2. 【USACO 2.1.5】海明码

    [题目描述] 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个 ...

  3. TIMESTAMP和DATETIME的区别

    TIMESTAMP和DATETIME的区别 1. 存储空间不同 a) TIMESTAMP占用4个字节 b) DATETIME占用8个字节 2. 受时区影响 c) TIMESTAMP实际记录的是1970 ...

  4. document.all和jq trigger原理

    document.all是页面内所有元素的一个集合.如:       document.all(0)表示页面内第一个元素document.all可以判断浏览器是否是IE     if(document ...

  5. HTTP协议学习-02

    HTTP 协议详解之 URL 的组成 http(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于 TCP 的连接方式,HTTP1.1 版本中给出一种持续连接的机制,绝大多数的 ...

  6. D题(贪心)

    D - D Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u     Descripti ...

  7. makefile简单介绍

    现在的IDE环境大多是高度集成的,只需要按一个按钮即可完成编译-汇编-链接的工作,但是实际在嵌入式开发的过程中,需要根据实际需要编写个性化的需求,这就需要掌握makefile的写法. 高级IDE的方便 ...

  8. System.Web.Http.Tracing 在webapi里面应用

    最近想写log.再接口里面 所以就想到了.net 4.0提供的这个类. 整好.配合asp.net api好使用 ,而且 本地调试会在 vs Output 里面输出. 1.开启这个Tracing con ...

  9. -_-#【Mac】快捷操作

    快捷键 command + 拖拽 = 剪切option + 拖拽 = 复制command + option + 拖拽 = 快捷方式 command + ] 前进command + [ 后退 comma ...

  10. cf591A Wizards' Duel

    A. Wizards' Duel time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...