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

  1. // stl/iterbug1.cpp
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7. int main()
  8. {
  9. vector<int> coll1; //empty collection
  10. vector<int> coll2; //empty collection
  11.  
  12. /* RUNTIME ERROR:
  13. * - beginning is behind the end of the range
  14. */
  15. vector<int>::iterator pos = coll1.begin();
  16. reverse (++pos, coll1 .end());
  17.  
  18. //insert elements from 1 to 9 into coll2
  19. for (int i=; i<=; ++i) {
  20. coll2.push_back (i);
  21. }
  22.  
  23. /*RUNTIME ERROR:
  24. * - overwriting nonexisting elements
  25. */
  26. copy (coll2.begin(), coll2.end(), //source
  27. coll1 .begin()) ; //destination
  28.  
  29. /* RUNTIME ERROR:
  30. * - collections mistaken
  31. * - begin() and end() mistaken
  32. */
  33. copy (coll1.begin(), coll2.end(), //source
  34. coll1. end()); //destination
  35. }

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. iOS9中将图片保存到照片中的某个相册的方法说明

    iOS9中将图片保存到照片中的某个相册的方法说明 在App中很经常遇到的就是用户点击某张图片后将图片保存到本地,下面介绍下iOS中保存图片的一些东西 1.首先,在iOS中把图片保存到系统照片是比较简单 ...

  2. POJ 2230 (欧拉路)

    分析: 基础的欧拉路算法,变化在于要求每条边正向和反向各走一遍. 链式前向星构图,只要标记走过的单向边,边找边输出即可. code #include <iostream> #include ...

  3. C#实现的异步Socket服务器

    介绍 我最近需要为一个.net项目准备一个内部线程通信机制. 项目有多个使用ASP.NET,Windows 表单和控制台应用程序的服务器和客户端构成. 考虑到实现的可能性,我下定决心要使用原生的soc ...

  4. ftp文件操作

    PHP FTP操作类( 上传.拷贝.移动.删除文件/创建目录 ) 2016-06-01 PHP编程 /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) */ class class_ ...

  5. sql编程 1

    declare emp_count number;begin select count(*) into emp_count from emp where HIOREDATE >= TO_DATE ...

  6. 【转】.NET开发者必备的11款免费工具

    摘要:本文介绍一些最适合.NET开发人员简化Web开发的最佳工具,这些工具都是开源的,有的开发工具你既可以从网上下载可执行文件,也可以下载源代码.一些工具拥有可扩展的框架,是一个持续集成工具. 原文链 ...

  7. python ATM购物程序

    需求: 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还,按欠 ...

  8. c程序代码的内存布局(学好C的基础)

    一个程序本质上都是由 BSS 段.data段.text段三个组成的.这样的概念在当前的计算机程序设计中是很重要的一个基本概念,而且在嵌入式系统的设计中也非常重要,牵涉到嵌入式系统运行时的内存大小分配, ...

  9. allegro 的光绘层概念

    TOP层: board geometry/outline  manufacturing/photoplot_outline                  etch/top              ...

  10. asp.net(C#)禁止缓存文件

    IIS会按文件地址及参数将文件缓存到客户端,以便再次访问该内容时速度更快.如果要取消这种机制则需要禁止缓存文件. 一.编程方式 Response.Buffer = true; Response.Exp ...