A summary of the list container in STL __stl

Source: Internet
Author: User

From:http://www.cnblogs.com/beyondanytime/archive/2012/08/10/2631191.html


1. About the list container

A list is a sequential container. The function of the list container is actually very similar to the bidirectional linked list in the data structure, the data element in list is a linear table in logical sense through the linked list pointer, which also has the main advantage of the list, that is, the insertion and deletion of elements in any position of the list is fast. The implementation of list is probably like this: Each node of the list has three domains: the precursor element pointer field, the data field, and the successor element pointer field. The predecessor element Pointer field holds the first address of the precursor element, the data field is the data of this node, and the successor element Pointer field holds the first address of the successor element. In fact, there are similar places in list and circulation list, namely: the predecessor of the head node of the element pointer field is saved is the first address of the tail element in the list, and the successor of the tail node of the lists holds the first address of the head node, so that the list actually constitutes a two-way circular chain. Because the list element node is not required to be in a contiguous memory, it is obvious that fast random access is not supported in the list, so for iterators, you can only move the iterator through the "+ +" or "--" operation to the successor/precursor node element. Instead of +n or-n operations on iterators, this is a different place from vectors.

I would like to put together three commonly used sequences to compare it is necessary:

vector: vector and built-in arrays, with a contiguous memory space, can be very good to support the immediate access, that is, the [] operator, but because its memory space is continuous, so inserting and deleting in the middle will result in a copy of the memory block, in addition, When more elements are inserted, the reserved memory space may not be sufficient, and a large enough memory needs to be requested again to copy the original data to the new memory space. These affect the vector's efficiency, but in fact most of the vector containers are used, and it is recommended that vector efficiency is generally good for most of the time. Vector usage analysis can refer to my another essay: http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/08/2627666.html

List : list is the data structure of the two-way list (according to SGI STL source code), so its memory space is discontinuous, through the pointer to access the data, this feature makes its immediate access to become very inefficient, so it does not provide the [] operator overload. However, because of the characteristics of the linked list, it can support the deletion and insertion of any place with good efficiency.

deque: deque is a double-ended queue, its implementation is not very clear, but know that it has the following two characteristics: it supports the [] operator, that is, to support the immediate access, and vector efficiency is almost the same, It supports operations at both ends: Push_back,push_front,pop_back,pop_front and so on, and the efficiency of the list at both ends of the operation is similar.

So in actual use, how to choose which of these three containers, should be based on your needs, you can follow the following principles :
1. If you need efficient and immediate access without caring about the efficiency of insertions and deletions, use vector
2. If you need a large number of inserts and deletes, but do not care about the immediate access, you should use the list
3. If you need immediate access and care about inserting and deleting data at both ends, you should use Deque.

the functions commonly used in 2.list

Constructors in 2.1list:

List () declares an empty list;

Listing (n) declares a list of n elements, each of which is constructed by its default constructor T ()

N,val declares a list of n elements, each of which is derived from its copy constructor, T (Val).

List (N,val) declares a listing that is the same as the above

List (first,last) declares an element whose initial value is derived from the elements in the sequence specified by the interval

2.2 begin () and End (): by calling the member function of the list container begin () to get a iterator to the beginning of the container, you can call the end () function of the list container to get the next position in the list, equivalent to: int a The n+1 position in [n] a[n] is actually non-existent, inaccessible, and often used as the end of the loop-end criterion.

2.3 push_back () and Push_front (): inserts an element into the list using the member functions of the list push_back and Push_front. where Push_back () is inserted from the end of the list, and Push_front () is inserted from the head of the list.

2.4 Empty (): use Empty () to determine whether the list is empty.

2.5 resize (): If you call Resize (n) to change the length of the list to accommodate only n elements, the exceeded elements will be deleted, and if you need to extend then call the default constructor T () to add the element to the end of the list. If resize (n,val) is invoked, the extension element calls the constructor T (Val) function to construct the element, the remainder being the same.

2.6 Clear (): clears all elements in the list.

2.7 Front () and Back (): the head element in the list container can be obtained by front (), and the last element of the list container can be obtained by back (). But one thing to note is what happens when the element in the list is empty, and then the call to Front () and back (). In fact, there will be no normal reading of data, but this is not an error, then we need to pay attention to the program, the individual feel that before using the best call empty () function to determine whether the list is empty.

2.8 Pop_back and Pop_front (): deletes the first element through Pop_front () by deleting the last element; the sequence must not be empty if pop_back () and Pop_front () are invoked when the list is empty Will cause the program to collapse.

2.9 Assign (): There are two things that are similar to the operations in vectors, the first of which is that l1.assign (N,val) converts the elements in L1 into N-T (val). The second scenario is: L1.assign (L2.begin (), L2.end ()) assigns values from L2.begin () to L2.end () in L2 to L1.

2.10 Swap (): swap two lists (two overloads), one is L1.swap (L2); The other is swap (L1,L2), which can all be exchanged for a linked list.

2.11 Reverse (): completes the list inversion by reverse ().

2.12 Merge (): merges two lists and makes them default ascending (also can be changed), L1.merge (l2,greater<int> ()); When the call ends, the L2 becomes empty, and the elements in the L1 contain the elements in the original L1 and L2 and are sorted in ascending order. In fact, the default is ascending,greater<int> () can be omitted, and greater<int> () can be changed, or not in ascending order.

Take a look at the following program: View Code

Run Result:

2.13 Insert (): inserts one or more elements (three overloads) at the specified location:

L1.insert (L1.begin (), 100); Inserts 100 at the beginning of the L1.

L1.insert (L1.begin (), 2,200); Inserts 2 100 at the beginning of the L1.

L1.insert (L1.begin (), L2.begin (), L2.end ()), and inserts the L1 element at the beginning of the L2 at all locations from the beginning to the end.

2.14 Erase (): deletes an element or an element of an area (two overloads)

L1.erase (L1.begin ()); Deletes the first element of the L1.

L1.erase (L1.begin (), L1.end ()); Deletes the L1 from the Begin () to end ().

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.