Chapter 9 ordered containers

Source: Internet
Author: User

The standard library of C ++ defines three sequential containers: vector, list, And deque (double-ended Queue) bidirectional queues.

The container only defines a small number of operations. Most additional operations are provided by the algorithm library.

9.1 Definitions of ordered containers

Include related header files

# Include <vector>

# Include <list>

# Include <deque>

All containers are class templates

Vector <int> ilist;

9.1.1 container element Initialization

1. containers can all use copies of the same type of containers.

Vector <int> list2 (ilist );

2. initialize to a copy of an element.

Vector <int> list3 (list2.begin (), list2.end ());

3. Allocate and initialize a specified number of elements

Vector <string> list4 (20, "hello") is initialized to 20 elements, each of which is hello

9.1.2 type constraints of elements in a container

Only elements that support Value assignment and object replication can use containers.

1. Special requirements for container operations

If the elements of the container are of the class type, note that some initialization methods require the class to have a default constructor.

2. Container

You can set the container type as a container, but spaces must be used for definition. Otherwise, it will be recognized as a stream operator by the compiler>

Vector <vector <string> lines,

9.2 scope of the iterator and iterator

The iterator of the vector and deque containers provides additional operations.

Add or subtract an integer n

Addition and subtraction between iterators

Greater than or equal

Note that the List container iterator only supports auto-increment and auto-increment operations.

9.2.1 iterator range

C ++ uses a bunch of iterators to mark the iterator range. The two iterators point to two elements in the same container, begin, end, first, and last respectively. Note, next element beyond the end

When the two iterators are equal, the container is empty. When the two iterators are different, the container must have at least one element.

9.2.2 is a container operation that fails the iterator

Note that each container defines one or more erase operations, which will invalidate the iterator. If the iterator fails, a serious running error occurs.

9.3 operations on ordered containers

Each container provides a set of useful types and operations, including adding and deleting elements, setting the container size, and obtaining the first and last elements in the container.

9.3.1 type alias defined by the container

Size_type unsigned shaping is sufficient to store the maximum possible container length of the container type

Iterator container iterator

Const_iterator read-only iterator

Reverse_iterator backward addressing iterator

Const_reveres_iterator read-only backward iterator

Difference_type is a signed integer that is sufficient to store the difference between two iterators.

Value_type element type

The left Value Type of the reference element, which is equivalent to value_type &

Constant left Value Type of the const_reference Element

9.3.2 begin and end members

. Begin () points to the first element of the container

. End () points to the next position of the last element of the container

. Rbegin () returns a reverse iterator pointing to the last element of the container.

. Rend returns the reverse iterator pointing to the previous position of the first element of the container.

9.3.3 add elements to the ordered container

. Push_back (t) adds an element whose value is t at the end of the container, and returns void

. Push_front (t) can be used only by list and deque. Add the element whose value is his from the container header, and return void

. Insert (P, T) inserts a new element with a value of T before the position pointed by P in the iterator, and returns the iterator pointing to the newly added element.

. Insert (p, N, T) inserts n new elements whose values are t before the position pointed by P in the iterator, and returns void

. Insert (p, B, E) inserts the element before iterator B to iterator E before the position pointed by iterator P, and returns void

Note that adding and deleting elements will invalidate the previously defined iterator. If such operations are available in recycling, the program must ensure that the iterator is updated after each loop to avoid storage. the iterator obtained by end () is also for this reason.

9.3.4 Relational operators

Containers of the same type support <> = operations. If the two containers have the same length and content, the two elements are equal. If the two containers have different lengths, the elements in a small container are equal to the corresponding elements in a long container, so the smaller container is smaller than the larger container. If the two containers have different sizes, then their size depends on the first element they are not equal

9.3.5 operations on container size

. Size () returns the element format of the current container. The return value type is size_type.

. Max_size () returns the maximum number of elements that can be stored in the current container.

. Resize (n) re-adjusts the container size so that it can accommodate n elements. If n is smaller than the previous maximum number of elements, the extra elements will be deleted, the value is used to initialize and add new elements.

. Resize (n, T) re-adjusts the container size so that it can accommodate n elements, and the value of all new elements is t

Note that the resize operation may invalidate the iterator.

9.3.6 access element

You can use the iterator to unreference and access

Vector and deque can be accessed by subscript or at (n ).

9.3.7 delete an element

. Erase (p) deletes the element pointed to by iterator P and returns an iterator pointing to the next element position of iterator p.

. Erase (B, E) deletes the elements between the two iterators and returns the next element of E.

. Clear () deletes all elements and returns void

. Pop_back () Delete the last element

. Pop_front () only list and deque support this operation. Delete the first element.

9.3.8 assignment and swap operations

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.