c++ stl library 学习(9)

来源:互联网 发布:拦截软件广告的软件 编辑:程序博客网 时间:2024/05/21 07:06

Requirements for Container Elements:

1. An element must be copyable by a copy constructor.能够被复制构造函数复制

2. An element must be assignable by the assignment operator能够被赋值操作符操作

3 An element must be destroyable by a destructor. Containers destroy their internal copies
of elements when these elements are removed from the container. Thus, the destructor
must not be private. Also, as usual in C++, a destructor must not throw; otherwise, all
bets are off.


容器中不能存放auto_ptr类型的元素。


元素的Value语义和Reference语义:

All containers create internal copies of their elements and return copies of those elements. This
means that container elements are equal but not identical to the objects you put into the
container. If you modify objects as elements of the container, you modify a copy, not the original
object

Copying values means that the STL containers provide value semantics. They contain the values
of the objects you insert rather than the objects themselves. In practice, however, you also need
reference semantics. This means that the containers contain references to the objects that are
their elements.Reference语义就是指容器包含能够指向容器内部元素的引用。

STL只支持Value语义不支持Reference语义。但是可以通过reference counting来实现Reference语义、


Errors and Exceptions Inside the STL:

Error Handling:

In particular, the use of the STL requires that the following be met:
• 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:
o Both iterators that specify a range must refer to the same container.
o 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.


Exception Handling:


原创粉丝点击