STL——container

1  STL的container分为两种类型

sequence  :元素存储的位置只和插入的顺序有关,和本身的值无关

associative:元素存储的位置只和本身的值有关,和插入顺序无关(对于这样的容器是不能作为Manipulation algorithm(修改值或位置)的destination,因为如果我们修改了其中的值或位置,那么容器就不是排序的了,这样对于查找而言将是很费时的。)

如下图所示,左边为sequence container,右边为associative container


C11里面有增加了一些container,如array,forward_list,unordered_map等


Vector:对于末尾的插入删除操作会非常快(如push_back, pop_back),但如果是在前面或中间位置进行插入或删除就会很慢(如insert),因为后面的所有元素都要移位


deque: double-end-queue,可以向两边增长,所有对于头尾的插入和删除都很快


2  容器适配器

STL提供了三个容器适配器:queue、priority_queue、stack。这些适配器都是包装了vector、list、deque中某个顺序容器的包装器。注意:适配器没有提供迭代器,也不能同时插入或删除多个元素。

以stack为例,

template < typename T, typename Container=deque > class stack;
可以使用三个标准顺序容器vecotr、deque、list中的任何一个作为stack的底层模型


3 容器中的element

container中的elemnt必须满足下面三个条件

1). An element must be  copyable by a copy constructor. The generated copy should be equivalent to the source. This means that any test for equality returns that both are equal and that both source and copy behave the same
2). An element must be assignable by the assignment operator. Containers and algorithms use assignment operators to overwrite old elementswith new elements. 
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


另外容器是支持值语义的,也就是说存入的是而不是引用,当然我们可以用指针来作为元素从而来模拟引用语义

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值