The list of STL

Source: Internet
Author: User

List is implemented by bidirectional linked list, the following mainly introduces some operation of STL list.

Description: For examples and function declarations not given, see http://www.cplusplus.com/reference/

Implementation: Bits/stl_list.h, BITS_STL/LIST.TCC (libstdc++ or include/c++) 1. Constructor, assign (), size (), max_size (), resize () 1. Constructor function

Explicit list (const allocator_type& alloc = Allocator_type ());

Explicit list (size_type N, const value_type& val = Value_type (),
                const allocator_type& alloc = Allocator_type ());

Template <class inputiterator>
list (Inputiterator, Inputiterator last,
        const allocator_type& Alloc = Allocator_type ());

List (const list& x);

The fourth one is the copy constructor, the remaining three except the Alloc parameter, others see example program. For the Alloc parameter, first look at the list's class template parameters:

Template < class T, class Alloc = allocator<t> > class list;
That is, the allocator type is specified through the class template parameter, and then the allocator object is specified by the constructor parameter.

Note: There are default values for both the class template parameters and the constructor parameters, and to clarify the differences and connections between the two. (Deque in the previous blog)

The default allocator object is Std::allocator, see g++ bits/stl_list.h in detail


2. Assign ()

Emplate <class inputiterator>
void Assign (Inputiterator-I, Inputiterator, last);

void Assign (size_type N, const value_type& val);
Like the constructor, note that the old data is all lost


3. Max_size ()

The maximum number of elements the list can hold, depending on the implementation of the system or library. Of course, it does not guarantee that the program will be able to achieve this value at runtime, such as the running process is not enough memory.

The role of size () is to return the number of elements in the current list
4. Resize ()

void Resize (size_type n, Value_type val = Value_type ());
Reduce or increase the number of elements in the list to Size_type N, and if you do not specify Val, the default values, such as the int default value of 0, similar constructors and assign ().


2. Front (), back (), Begin (), End ()

1. Front (), Back () returns a reference, while Begin (), End () returns an iterator (pointer), see function declaration.

2. Back () points to the last element, and end () points to the rear of the last element to determine whether it is an endpoint.


3. Insert (), erase (), remove (), remove_if (), Unique ( ) 1. Insert ( )

Just note that the inserted element is before the element that the given iterator points to (inserts given value into%list before specified iterator).


2. Remove (), remove_if (), Erase ()

Erase () is the deletion of the specified node with the iterator, remove () is deleted by value (operator==) equality, remove_if () is the specified condition deletion.


3. Unique ()

void unique ();

Template <class binarypredicate>
void Unique (binarypredicate binary_pred);
Deletes duplicate elements, leaving only the first one. Either follow the operator== process or follow the rules specified by binary_pred.


4. Sort ()

void sort ();

Template <class compare>
void sort (Compare comp);
1. If you do not specify a comparison rule, the operator&lt is used by default, the sorting result is ascending, and if specified, the specified rule.

2. Sorts the elements of this list in Nlogn time. Equivalent elements remain in the list order (sort is stable).


5. Splice (), merge () 1 Splice ( )

Splice () is stitching from the specified location, regardless of order (note: not split);


2. Merge ()

void merge (list& x);

Template <class compare>
void Merge (list& x, Compare comp);
1. If two lists are sorted, the merged list is ordered in the same order. The rule is either the default OPERATOR&LT, or the specified (same sort ()). If disorder, then the merger is also disorderly, this case splice ().

2. For the equal element this is in the front, x after.

3. The operation completes the Hu X is empty.

4. The function does nothing if (&x = = this).


6. Swap (), reverse ()

Swap (): Swap two list (see example), this method has two versions, one is a member function, one is a non-member function

Reverse (): Reverse list


7. Overloaded Relational comparison operations (non-member functions)

Like the deque described in the previous posting (http://blog.csdn.net/duyiwuer2009/article/details/23469729), this comparison rule is lexicographical_compare ( http://blog.csdn.net/duyiwuer2009/article/details/23277803).




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.