List of STL

Source: Internet
Author: User
Tags constructor
A summary of the list container in STL

1. About the list container

List is a sequence container. The function of the list container is actually very similar to the doubly linked list in the data structure, and the data element in list is the linear table in logical sense through the chain list pointer, that is to say list has the main advantage of the list, namely: the insertion and deletion of elements in any position of the linked list is fast. The implementation of list is probably this: each node of the list has three fields: the precursor element pointer field, the data field, and the successor element pointer field. The precursor 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, the list and the circular link list also has the similar place, namely: the head node's precursor element pointer field holds the tail element in the chain list the first address, the list's tail node's successor element Pointer field holds the head node's first address, thus, the list actually constitutes a bidirectional loop chain. Since 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, the iterator can be moved to the successor/precursor element only through the "+ +" or "--" operation. Instead of the +n or-n operation of Iterators, this is different from vectors.

I would like to compare the three commonly used sequences in a way that is necessary:

vectors: vectors are similar to built-in arrays, have a contiguous memory space, can be very well supported immediately access, that is, the [] operator, but because its memory space is continuous, so in the middle of the insert and delete will cause a copy of the memory block, in addition, When more elements are inserted, the reserved memory space may not be sufficient to request a large enough memory and copy the original data to the new memory space. These affect the efficiency of the vector, but in fact the most used is the vector container, it is recommended that most of the time the use of vector efficiency is generally good. The use of vector analysis can refer to my other essay: http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/08/2627666.html

list: list is a doubly linked list in the data structure (according to the SGI STL source code), so its memory space is discontinuous, through the pointer to the data access, this feature makes its immediate access to become very inefficient, so it does not provide overloading of the [] operator. However, due to the characteristics of the list, it can be very efficient to support the deletion and insertion anywhere.

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, which is the support immediately access, and the efficiency of the vector is similar, It supports operations at both ends: Push_back,push_front,pop_back,pop_front and so on, and the efficiency of the list on both sides of the operation is similar.

Therefore, in the actual use, how to choose which of the three containers, should be based on your needs, depending on the following principles can be followed:
1. If you need efficient immediate access, instead of the efficiency of INSERT and delete, use vector
2. If you need a large number of insertions and deletions and do not care to access them immediately, you should use the list
3. If you need to immediately access and care about the insertion and deletion of data at both ends, you should use Deque.

functions commonly used in 2.list

Constructors in 2.1list:

List () declares an empty listing;

Lists (n) declare a list of n elements, each of which is constructed by its default constructor, T ().

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

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

List (first,last) declares an element whose initial value is derived from the element 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 that points to the beginning of the container, you can call the end () function of the list container to get the next position of the list end, equivalent to: int a The n+1 position in [n] a[n], which is not actually present, is not accessible and is often used as the end-of-loop judgment ending condition.

2.3 push_back () and Push_front (): inserts an element into the list using the list's member function push_back and Push_front. where Push_back () is inserted from the end of the list, and Push_front () is implemented 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 only n elements, the elements that are exceeded are deleted, and if you need to expand then call the default constructor T () to add the element to the list end. If you call resize (n,val), the extension element is called by the constructor T (Val) function to construct the element, and the remainder is 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 through front (), and the last element of the list container can be obtained by back (). But one thing to note is that when the element in the list is empty, what happens when you call front () and back (). In fact, the data can not be read normally, but this is not an error, then we have to make a program to pay attention to, the individual think before using the best call empty () function to determine whether the list is empty.

2.8 Pop_back and Pop_front (): removes the first element by removing the last element, by Pop_front (), and the sequence must not be empty if the list is empty when the Pop_back () and Pop_front () are called Will cause the program to be blown away.

2.9 Assign (): similar to the operation in the vector, there are two cases, the first is: L1.assign (n,val) changes the L1 element to n T (Val). The second case is: L1.assign (L2.begin (), L2.end ()) assigns a value from L2.begin () to L2.end () in L2 to L1.

2.10 Swap (): Exchange two lists (two overloads), one is L1.swap (L2); The other is swap (L1,L2), which can complete the exchange of even a linked list.

2.11 Reverse (): completes the inverse of the list by reverse ().

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

Take a look at the following program:

1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main ()
 7 {
 8     list<int> L1;
 9     list<int> L2 (2,0);
Ten     list<int>::iterator iter;     L1.push_back (1);     L1.push_back (2);     L2.push_back (3);     L1.merge (l2,greater<int> ());//after merging in ascending order, the default is the ascending     (iter = L1.begin (); iter = L1.end (); iter++)         , cout<<*iter<< "";     cout<<endl<<endl;     if (L2.empty ())     {         cout<< "L2 becomes empty. ";     cout<<endl<<endl;     return 0;
26}

Operation Result:

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

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

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

L1.insert (L1.begin (), L2.begin (), L2.end ()), the element that inserts the L1 from start to end at the beginning of the L2.

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

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

L1.erase (L1.begin (), L1.end ()); Removes the element from begin () to end () L1.

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.