[STL Basics] ordered containers

Source: Internet
Author: User
Document directory
  • Vector
  • List
  • Deque
Container

A collection of objects of the same type. Common member functions of these containers: empty () determines whether there are elements in the container. max_size () indicates the maximum number of elements that can be loaded in the container. swap () exchanges the content of the two containers.

Compare the two container size operators: =, <, <=,>, >=, = ,! =

  • 1. If the two containers have the same length and all elements are equal, the two containers are equal. Otherwise, they are unequal.
  • 2. If the two containers have different lengths, but all elements in the shorter container are equal to the corresponding elements in the longer container, the shorter container is smaller than the other container.
  • 3. If neither container is a sub-sequence of the other container, it depends on the first element to be compared)
Linear table

In practical applications, linear tables are strings, arrays, stack stacks, queue, priority queue priority_queue, vector, list, two-way queue deque and other special linear tables. The storage methods can be divided into sequence tables and linked lists. linked lists can be further divided into single-chain tables, double-chain tables, and cyclic linked lists.

Ordered container (vector, list, deque)

The order of elements is independent of the element values, but determined by the order in which the elements are added to the container.

  • 1. Define assignment. Their definition assignment assign and usage are identical.
  • 2. both the front () and back () elements at the end of the table element list are returned. For the two-way linked list and two-way queue deque, there is only one header and one table at the end, the two-way method is used to add and delete elements in two-way.
  • 3. the Methods push_back () and pop_back () are added at the end of the table. The methods insert () and erase () are added to any position (), delete all clear () elements ().
  • 4. return iterator and reverse_iterator. Return the begin () pointer to the table Header element, return the next end () of the table tail element, and return the rbegin () pointer to the table tail element (), returns the rend () position before the element in the header ().
  • 5. For others, obtain the length size (), reset the length resize (), return the maximum length max_size (), switch swap () between the two containers, and determine whether empty () is empty ()
Vector

Vector is a dynamic array. Sequential storage (index value, at () can be used for element access, and its iterator can +-n), continuous storage of elements. More than list and deque, you can obtain capacity () and set the minimum capacity reserve ()

List

List is a two-way linked list. Chained storage (index values cannot be used for access, and its iterator cannot be +-n) is a bidirectional linked list composed of nodes. Each node contains an element. Because it is a two-way linked list, more functions such as push_front () and pop_front () are provided in the header, and more sorting functions such as sort () are provided in addition to vector and deque (), merge queue merge () (Note: sort before merge), join function splice (), deduplicate function unique (), delete element function remove (), remove_if ()

Deque

Two-way queue deque. Sequential storage (index value, at () can be used for element access, and its iterator can +-n), continuously stores arrays composed of pointers pointing to different elements. Because it is a two-way queue, more functions such as push_front () and pop_front () are provided in the header, and the reverse function reverse () is provided less than that in the vector ()

Rules for selecting container types

If the program requires Random Access to elements, the vector or deque container should be used.
If the program must insert or delete elements in the middle of the container, the list container should be used.
Deque container should be used if the program inserts or deletes elements not in the middle of the container but in the header or tail of the container.
If you only need to insert elements in the middle of the container when reading the input, and then randomly access the elements, you can consider reading the elements into a list container during the input, and then re-sorting the container, make it suitable for sequential access, and then copy the sorted list container to a vector container.

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.