c++ stl library 学习(9)

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:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值