"C + + STL" List

Source: Internet
Author: User

1. Structure

List uses a double linked list (a doubly linked list) to manage elements.

2. List ability

The internal structure of the list is very different from the vector or deque, so it differs from them:

The list does not support random access, it needs to access an element, it needs to traverse all the previous elements, and it is very slow behavior.

The placement and deletion of elements in any location (not just on both ends) is very fast and is always done in constant time, as there is no need to move any other action, actually only a few pointers.

Placement and deletion do not invalidate individual pointers, reference, iterators that point to other elements

A list is an atomic operation that succeeds or fails, and does not say only half of the execution.

The list does not support random access and does not provide the subscript operator and the at () function.

The list does not provide an operational function for capacity, memory allocation, because it is completely unnecessary, and each element has its own memory space, which is valid until it is deleted.

The list provides specialized functions for moving functions that perform efficiently without copying and moving elements, and only need to adjust several pointers.

3. Operation function 3.1 Construction and destruction

Operation

Effect

List<elem> C

To generate an empty list

List<elem> C1 (C2)

Produces a C2 list with each element copied

List<elem> C (N)

Produces a list of n elements, each of which is generated by the default construct

List<elem> C (N,elem)

Produces a list of n elements, each of which is a copy of the Elem

List<elem> C (beg,end)

Produces a list with all elements in the interval [beg,end] as the initial value

C.~list<elem> ()

Destroy all elements and free up memory

3.2 Non-volatile operation

Operation

Effect

C.size ()

Returns the current number of elements

C.empty ()

Determines whether the size is zero, equal to 0 = = size (), higher efficiency

C.max_size ()

Returns the maximum number of elements that can be accommodated

C1 = = C2

Determine if C1 equals C2

C1! = C2

Determine if C1 is not equal to C2 (equal to!) ( C1==C2))

C1 < C2

Determine if C1 is less than C2

C1 > C2

Determine if C1 is greater than C2

C1 <= C2

Determine if C1 is less than or equal to C2 (equal to!) ( C2<C1))

C1 >= C2

Determine if C1 is greater than or equal to C2 (equal to!) ( C1<C2))

3.3 Assigning values

Operation

Effect

C1 = C2

Assign all elements of C2 to C1

C.assign (N,elem)

Copy n copies of Elem to C

C.assign (Beg,end)

Create a list, all elements within the interval [beg,end] as the initial value

C1.swap (C2)

C1 and C2 Element interchange

Swap (C1,C2)

C1 and C2 element interchange, global functions

3.3 Element Access

List does not support random access, only front () and back () can access elements directly.

Operation

Effect

C.front ()

Returns the first element without checking for the existence of an element

C.back ()

Returns the last element without checking that the element exists

3.4 Iterator-related functions

List only uses iterators to access elements, and list does not support random access, so these iterators are bidirectional iterators, which are not used by algorithms that use random access iterators.

Operation

Effect

C.begin ()

Returns a random access iterator that points to the first element

C.end ()

Returns a random access iterator that points to the next position of the last element

C.rbegin ()

Returns a reverse iterator that points to the first element of a reverse iteration

C.rend ()

Returns a reverse iterator that points to the next position of the last element of the reverse iteration

3.5 placement and removal of elements

List provides all the features of deque, plus the addition of remove () and remove_if () to the list.

Operation

Effect

C.insert (POS, Elem)

Inserts a elem copy at the iterator POS location, returning the new element position

C.insert (Pos,n, Elem)

Inserts n elem copies at the iterator POS location with no return value

C.insert (POS, Beg,end)

Inserts a copy of all elements in the interval [beg,end] at the position of the iterator POS, no return value

C.push_back (Elem)

Append a copy of Elem to the tail

C.pop_back ()

Removes the last element and does not return

C.push_front (Elem)

Insert a elem copy in the head

C.pop_front ()

Removes the first element without returning a

C.remove (Val)

Remove all elements with a value of Val

C.remove_if (OP)

Removes all elements that cause op (elem) to be true

C.erase (POS)

Removes the element referred to by the iterator Pos, returning the next element position

C.erase (Beg,end)

Removes all elements within the interval [Beg,end], returning the next element position

C.resize (num)

Resets the element capacity to NUM, and if size becomes larger, constructs all elements with the default constructor

C.resize (num, elem)

Resets the element capacity to NUM, and if size is larger, all elements are copies of Elem

C. Clear ()

Remove all elements to empty the entire container

3.6 Special Variable operation

Operation

Effect

C.unique ()

If there are several adjacent elements with equal values, remove the duplicate elements and leave only one

C.unique (OP)

If there are several adjacent elements, make op () True, remove the repeating element, leaving only one

C1.splice (POS,C2)

Move all C2 elements before the C1 container POS iterator

C1.splice (Pos,c2,c2pos)

Move the C2 pos position element to the C1 element POS location, C1 and C2 can be the same

C1.splice (Pos,c2,c2beg,c2end)

C1 and C2 can be the same before moving all elements of the C2 interval [c2beg,c2end] to the C1 POS location

C.sort ()

Sort all elements with operator <

C.sort (OP)

With OP (), sort C

C1.merge (C2)

Assuming C1 and C2 are ordered, move the C2 element to C1 and ensure that the merged list is still ordered

C1.merge (C2,OP)

Assuming that both C1 and C2 are in OP () order, the C2 is moved to C1 still in OP () ordered

C.reverse ()

Reverse all the elements

4. Sample Code
 //Cont/list1.cpp#include<iostream>#include<list>#include<algorithm>using namespacestd; voidPrintlists (Constlist<int>& One,Constlist<int>& A) {cout<<"List1:"; Copy (L1.begin (), L1.end (), Ostream_iterator<int> (cout," ")); cout<< Endl <<"List2:"; Copy ( A. Begin (), A. End (), ostream_iterator<int> (cout," ")); cout<< Endl <<Endl; }    intMain () {//Create both empty listslist<int>List1, List2; //fill both lists with elements         for(intI=0; i<6; ++i) {list1.push_back (i);        List2.push_front (i);        } printlists (List1, List2); //Insert all elements of List1 before the first element with value 3 of List2//-find () returns an iterator to the first element with value 3List2.splice (Find (List2.begin (), List2.end (),//Destination Position                          3), List1); //Source listprintlists (List1, List2); //Move first element to the endList2.splice (List2.end (),//Destination PositionList2,//Source listList2.begin ());//Source Positionprintlists (List1, List2); //Sort second list, assign to List1 and remove duplicatesList2.sort (); List1=List2;        List2.unique ();        Printlists (List1, List2); //Merge both sorted lists into the first listList1.merge (LIST2);     Printlists (List1, List2); }

Output:

List1:0 1 2 3 4 5List2:5 4 3 2 1 0List1:list2:5 4 0 1 2 3 4 5 3 2 1 0List1:list2:4 0 1 2 3 4 5 3 2 1 0 5List1:0 0 1 1 2 2 3 3 4 4 5 5List2:0 1 2 3 4 5List1:0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5List2:

"C + + STL" List

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.