c++ stl library 学习(6)

Sequence Containers:

Vectors:

vector<int> coll;//初始化时为空,没有元素。

push_back()

.size()

By using the subscript operator [], you can access a single element of a vector:

coll[i ]

random access available


Deques:It is a dynamic array that is implemented so that it can grow in both directions. Thus, inserting elements
at the end and at the beginning is fast. However, inserting elements in the middle takes time because elements must be moved.

deque<float> coll;

coll.push_front();

coll.push_back();

random access available

[ ]is available;


list:a doubly linked list of elements//双向链表构成

random access unavailable,so access is slower than vector and deque

insert or remove is faster than vector and deque。

coll.front() //return the first element

coll.pop_front()// erase the first element

coll.empty()//isempty

不能使用[ ]来获取元素


strings:objects of the C++ string classes,Strings are similar to vectors except that their elements are characters.
(basic_string<>, string, and wstring)


Ordinary Arrays:is not a STL's container,but STL's design allows you to call algorithms for these ordinary arrays. This is especially useful
when you process static arrays of values as an initializer list.


Associative Containers: Associative containers are typically implemented as binary trees.任何节点的左子树元素都小于右子树元素。

。不能使用push_back\push_front,因为你不知道你没有权利指定新元素的位置,因为它们是内部自动排序的。

By default, the containers compare the elements or the keys with operator

关联容器的不同之处在于它们支持的元素类型不同以及处理重复元素的方式不同。

Sets
A set is a collection in which elements are sorted according to their own values. Each
element may occur only once, thus duplicates are not allowed.
• Multisets
A multiset is the same as a set except that duplicates are allowed. Thus, a multiset
may contain multiple elements that have the same value.
• Maps//可以使用[ key]操作符构成关联数组。它的key可以使任何类型,
A map contains elements that are key/value pairs. Each element has a key that is the
basis for the sorting criterion and a value. Each key may occur only once, thus duplicate
keys are not allowed. A map can also be used as an associative array, which is an array
that has an arbitrary index type (see page 91 for details).
• Multimaps 不可以使用[ ],因为它的key可以重复存在,你将不知道将其赋值给谁
A multimap is the same as a map except that duplicates are allowed. Thus, a multimap
may contain multiple elements that have the same key. A multimap can also be used as
dictionary. See page 209 for an example

如果两者都不小于对方,则表示两者相等。

You can consider a set as a special kind of map, in which the value is identical to the key.//set实际是一种特殊的map,它的键值就是它的value。

实际运用中,所有的关联容器都是由最基本的二叉数组成。



Container Adapters:Container adapters are historically part of the STL. However, from a programmer's view point,
they are just special containers that use the general framework of the containers, iterators, and
algorithms provided by the STL. Therefore, container adapters are described apart from the STL

• Stacks
The name says it all. A stack is a container that manages its elements by the LIFO (lastin-
first-out) policy.
• Queues
A queue is a container that manages its elements by the FIFO (first-in-first-out) policy.
That is, it is an ordinary buffer.
• Priority Queues
A priority queue is a container in which the elements may have different priorities. The
priority is based on a sorting criterion that the programmer may provide (by default,
operator < is used). A priority queue is, in effect, a buffer in which the next element is

always the element that has the highest priority inside the queue. If more than one
element has the highest priority, the order of these elements is undefined


Iterators:迭代器是一个可以遍历所有序列中元素的一个对象,它指出容器中元素的位置。每一个容器都有2个迭代器:container::iterator 、container: : const_iterator。

• Operator *
Returns the element of the actual position. If the elements have members, you can use
operator -> to access those members directly from the iterator. [5]
[5] In some older environments, operator -> might not work yet for iterators.
• Operator ++
Lets the iterator step forward to the next element. Most iterators also allow stepping
backward by using operator --.
• Operators == and !=
Return whether two iterators represent the same position.
• Operator =
Assigns an iterator (the position of the element to which it refers).
<

• begin()
Returns an iterator that represents the beginning of the elements in the container. The
beginning is the position of the first element (if any).

• end()
Returns an iterator that represents the end of the elements in the container. The end is
the position behind the last element. Such an iterator is also called a past-the-end
iterator.

For empty ranges, begin() is equal to end()


Note that the preincrement operator (prefix ++) is used here. This is because it might have better
performance than the postincrement operator. The latter involves a temporary object because it
must return the old position of the iterator. For this reason, it generally is best to prefer ++pos
over pos++.// ++pos 比pos++ 快些。

1. Bidirectional iterator
As the name indicates, bidirectional iterators are able to iterate in two directions: forward,
by using the increment operator, and backward, by using the decrement operator. The
iterators of the container classes list, set, multiset, map, and multimap are
bidirectional iterators.
2. Random access iterator
Random access iterators have all the properties of bidirectional iterators. In addition, they
can perform random access. In particular, they provide operators for "iterator arithmetic"
(in accordance with "pointer arithmetic" of an ordinary pointer). You can add and subtract
offsets, process differences, and compare iterators by using relational operators such as
< and >. The iterators of the container classes vector and deque, and iterators of
strings are random access iterators.


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值