Precautions for STL ordered containers

Source: Internet
Author: User

A sequential container is a vector, list, And deque.

1. Constructor

If C is one of the preceding three containers, the constructor has the following five types:

1. c <t> C; // T is an element type, and the container Initialization is empty. For example, vector <int> VEC;

2. c <t> CC (c); // create a container CC and copy the value of C to CC. the two must have the same container type. for example, an error occurs when vector <int> VEC; vector <double> vt (VEC );

3. c <t> C (START, end); // create a container and copy the elements from the two iterator intervals of the other container. the two containers can be different. however, element types must be matched.

// For example, vector <int> VEC; List <int> LT (VEC. Begin (), VEC. End ());

4. c <t> C (n, Val); // create a container with N values. Each value is Val. The Val type does not have to be the same as t, but must be convertible. for example, vector <int> VEC (3, 1.0 );

5. c <t> C (n); // create a container with N values. All values are 0 by default.

Supplement: in fact, the first three constructor types can be used for correlated windows, but four or five constructor types can only be used for sequential containers.

In addition, there are only two constructor types for the container adapter.

Queue <int> qu; // method 1

Queue <int> que (Qu. _ get_container); // copy Qu. because queue is actually implemented using deque. so this constructor is equivalent. deque <int> que (qu );

 

2. Container element type constraints

All built-in types can be used as container elements, but the only exception is reference (it does not support assignments in the general sense). pointers can also be used as container elements. if the custom type is set, the following two conditions must be met:

Value assignment is supported.

Type objects must be reproducible.

 

 

3. Special usage of vector and deque iterator

The container adapter (stack, queue, priority_queue) does not have an iterator, but both the sequential container and the associated container have an iterator. The general iterator only supports the following simple operations:

* Iterator, iterator-> item,

Iterator ++, iterator --, ++ iterator, -- iterator,

Iterator1! = Iterator2, iterator1 = iterator2

 

However, the iterators of the vector and deque containers have some special usage, because both of them are implemented using dynamic arrays, so their iterators will perform operations similar to the array subscript.

It has the following special usage: (In summary, there are two main categories: arithmetic operations and size comparison)

Iterator + N or iterator-N // position of the N-bit or N-bit element after the iterator

Iter1 + = iter2

Iter1-iter2;

>,>=,<,<=// The array subscript is later than the previous subscript.

 

The array subscript is a number arranged from small to large, and the iterator of vector and deque is similar to the array subscript. In this way, it is easy to understand the special usage above.

 

Iterator failed

The iterator is similar to a pointer. We know that if the memory area pointed to by the pointer is released, the pointer becomes a wild pointer. similarly, if the iterator points to an element that has been changed (deleted or moved), such as pop_back () or erase. when the iterator is used, it cannot be determined whether it is invalid. so you have to be careful when using it.

 

 

Container Selection

Generally, we can use vector without special requirements.

If you need to insert or delete data frequently, you can use the list, that is, you need random access, and you need to insert or delete data at the beginning and end to use deque.

If you only need to perform some insert operations when initializing the container, and the subsequent operations are all access elements, You can first initialize the elements in the list and then copy the elements in the list to the vector.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.