C++为什么remove以后需要erase

"Why don’t algorithms call erase() by themselves? This question highlights the price of the flexibility of the STL. The STL separates data structures and algorithms by using iterators as the interface.However, iterators are an abstraction to represent a position in a container. In general, iterators do  not know their containers. Thus, the algorithms, which use the iterators to access the elements of the container, can’t call any member function for it.
This design has important consequences because it allows algorithms to operate on ranges that are different from “all elements of a container.” For example, the range might be a subset of all elements of a collection. The range might even be a container that provides no
erase() member function (an array is an example of such a container). So, to make algorithms as flexible as possible, there are good reasons not to require that iterators know their container.

Note that often it is not necessary to remove the “removed” elements. Often, it is no problem to use the returned new logical end instead of the real end of the container. In particular, you can call all algorithms with the new logical end. "

        ---<<The C++ Standard Library A Tutorial and Reference (2nd Edition)>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值