STL containers and use a simple summary

STL (Standard Template Library) aims to standardize components, so do not re-development, you can use ready-made components. STL is part of C ++, so do not install additional libraries.

STL is a generic programming. Object-oriented programming is concerned that the data aspects of programming, and generic programming algorithm is concerned. (not understand well)

STL includes container (containers) and algorithms (algorithms), the following is his understanding of several major STL containers.

STL containers

 

vector

Vector, contiguous storage space can be used [] operator. A dynamically allocated storage space of the container, after insertion of a new element, automatically redistribution (expansion) space.

Can quickly insert elements at the end, but in the middle of the sequence of insert / delete elements slower, and if space is allocated a start is not enough, there is a re-allocation of more space, it will have a copy of the performance overhead.

list

Doubly linked list, each inter-element is connected with a chain, it may be non-contiguous storage space. You can not use the [] operator.

Access random elements as fast vector, random insertion elements faster than the vector, for each element of distribution space, not enough space, reallocation of cases it does not exist. So the list is more suitable circumstances require anywhere frequent insertion / deletion in.

queue

Unidirectional queue is a FIFO of the FIFO, can use the [] operator.

and

Deque, may be used [] operator.

Queue unidirectional push, unidirectional POP; deque is the way (either from a front direction, a direction from the back) can push and pop.

Small pieces of continuous, small pieces are connected with the chain, in fact, a map of the internal pointer.

Fast at the beginning and end of the insert elements; random insertion / deletion of elements to be slow; fast reallocation than vector space.

set

Set associative containers, only key values, elements unique must be unique.

Internal use a balanced tree structure to store, each insert will re-sort, search is faster

Reference "C ++ Primer" P378 type requires a keyword, by default, the standard key type of library <operator to compare two keywords. For the set and map, in accordance with this <to sort of relationship.

We can not directly define a set of self-defined type or map, because it has no <operator, needs its own devoted to this topic again on the provisions of a custom type <operator, have the opportunity.

mutilset

Element allows repeat

map

Binding associated with the container, one to one mapping <key, value>, key can not be repeated.

Elements can be inserted through the insert (std :: pair <key, value>), or directly [key] = value insertion.

It can be used directly [key] to give the value of the element value.

For the set and map, to get used to using an iterator iterator.

multimap

Key values ​​can be repeated

C array are successively stored in the speech, the main difference is that the array vector and the vector can be increased at any length, but the length of the array is determined when the definition of a good.

Vector

Array

Size can get the length of the vector

You can not be acquired, when the length of the definition has been determined

Fixed length, can be increased at any time

Fixed length can not change the definition

Can be added at the end of the element vector (with push_back)

It can not be increased in length than the length of the

Can not determine the length, you must define a lot of space left when defining the array, resulting in a waste of memory

It may determine the length of the space-saving

Size can get the length of the vector

You can not be acquired, when the length of the definition has been determined

Vector, list, queue, deque container belong to the order, how to choose what kind of container it?

Here are some guidelines to select the order container type

  1. If we need random access to a container, the vector is much better than a list.
  2. If we want to store the number of known elements, the vector is a better choice than a list.
  3. If we need more than just across the container insert and delete elements, then the list is clearly better than vector good.
  4. Unless we need in container header insert and delete elements, vector or deque than good.
  5. If only the easy insertion of the header and trailer data element is selected deque.

Original link: https://blog.csdn.net/fx677588/article/details/52794015

 

Sample code and use the various functions of STL containers can be found directly from the Internet, it is recommended that both URLs:

http://www.cplusplus.com/

https://en.cppreference.com/w/c

需要注意的是,在使用set和map时,因为key是唯一的,如果你insert了相同key值的元素,则会覆盖掉之前的元素(map的value值变成新插入的)。所以,为了避免不必要的debug工作,养成好的编码习惯:

  1. 在往set或map中插入新元素之前,先check是否已经有相同key的元素了,如果有,就assert。
    std::map<key type, value type>::iterator it = m_map.find( key_element );
    assert(it ==m_map.end());
    m_map[key_element] = value ;

     

  2. 在准备erase set或map中的元素时,先check是否存在这个key的元素。
    std::map<key type, value type>::iterator it = m_map.find( key_element );
    assert(it !=m_map.end());
    m_map.erase(it);

    同时,在使用STL容器的时候,一定要特别注意,有insert就需要有erase,否则内存会爆掉的。

发布了8 篇原创文章 · 获赞 0 · 访问量 176

Guess you like

Origin blog.csdn.net/zgcjaxj/article/details/104909354