您的位置:首页 > 大数据 > 人工智能

Chaper 11. Associate Container

2015-09-03 12:12 417 查看

1.

The associative containers do not support the sequential-container position-specific operations, suchas
push_front
or back. 

The associative containers do not support the constructors or insert operations that take anelement value and a count. 

The associative container iterators are bidirectional.

2.

When we initialize a
map, we have to supply both the key and the value. We wrap each key–value pair inside curly braces:

{key,
value} 

3.

When we dereference an iterator, we get a reference to a value of the container’svalue_type.
In the case of map, the
value_type
is a pair
in which
first holdsthe
const
key and second
holds the value 

4.

Although the
set types define both the
iterator
and const_iterator
types, both types of iterators give us read-only access to the elements in the
set. Just as we cannot change the key part of a
map
element, the keys in a
set are also
const. Wecan use a
set
iterator to read, but not write, an element’s value.

5.

When we use an iteratorto traverse a
map, multimap, set, or
multiset, the iterators yieldelements in ascending key order. 

6.

For the containers that have unique keys, the versions of
insert
and emplace
that add a single element return a
pair
that lets us know whether the insertion happened. The
first
member of the pair
is an iterator to the element with the given key; the
second
is a bool
indicating whether that element was inserted, or was already there. If the key is already in the container, then
insert does nothing, and the
bool
portion of the return value is
false. If the key isn’t present, then the element is inserted and the
bool
is true. 

For the containers that allow multiple keys, the
insert
operation that takes a singleelement returns an iterator to the new element. 

7.

Today I was trying to use:
auto ret = iset.insert(std::advance(itr, 3), 1);
Compiler complained an error. The reason is that the function returns void. itr is a reference parameter. That's how its value gets changed.

8.

we can erase
one element or a rangeof elements by passing
erase
an iterator or an iterator pair.

9.

Because the subscript operator might insert an element, we may use subscript only ona
map
that is not const. 

10.P547

c.find(k), c.count(k), c.lower_bound(k), c.upper_bound(k), c.equal_range(k)

11.

We are guaranteed that iterating across a
multimap
or multiset
returnsall the elements with a given key in sequence. 

12.

If the key is in the container, the iterator returned from
lower_bound
will refer to the first instance ofthat key and the iterator returned by
upper_bound
will refer just after the last instance of the key. 

If the element is not in the
multimap, then
lower_bound
andupper_bound
will return equal iterators; both will refer to the point at which the key can be inserted without disrupting the order. 

This function
equal_range takes a key and returns a
pair
of iterators. If the key is present, then the first iterator refers to the first instance of the key and the second iterator refers one past the last instance of the key. If no matching element is found, then
both the first and second iterators refer to the position where this key can be inserted. 

13.

Use an unordered container if the key type is inherently unordered or ifperformance testing reveals problems that hashing might solve.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: