STL Standard Library of C + + learning Notes (iv) List

Source: Internet
Author: User

The template class list is a container, and the sequence of the controlled length n is a doubly linked list with n nodes. Each node stores a single element, a precursor pointer, and a successor pointer. List nodes are stored in non-contiguous storage space, because of its structure, the insertion, deletion, substitution and other operations are very fast, but the elements of the search and random access operations are very slow, you need a node from the head node of a node to look down.

Include header file #include <list> Declaration namespace using namespace Std;

1) constructor function

  List (); Declare an empty list, such as:list<int> A;

List (int nSize); Create an empty list with the number of elements nsize such as:list<int> A (10);

List (int nsize,const t& val); Create a list with an element number of nsize and a value of Val such as:list<string> a (5, "has fun");

List (const list&); Copy (copy) constructor

such as:list<string> A (5, "has fun");

List<string> B (a);

  List (begin,end); Copy elements from another sequence [begin,end] range to list

such as: intia[6] = { -2, -1, 0, 1, 2, 1024 }; 

      list<int> a(ia,ia+6);

2) Fast interpolation value function

  void Push_front (const t& val); Add an element to the head of the list Val

such as: List<double> A (5,10.1);
A.push_front (5.1);
List<double>::iterator ITER;
For (Iter=a.begin (); Iter!=a.end (); iter++)
{
cout<<*iter<<endl;
}

Program output:

void Push_back (const t& val); Add an element to the tail of the list Val

such as: List<double> A (5,10.1);
A.push_back (5.1);
List<double>::iterator ITER;
For (Iter=a.begin (); Iter!=a.end (); iter++)
{
cout<<*iter<<endl;
}

Program output:

void Assign (Size_type n,const t& x); Resets the list to a new list of n elements that are set to a value of X

such as: List<double> A (5,10.1);
A.assign (2,5.1);
List<double>::iterator ITER;
For (Iter=a.begin (); Iter!=a.end (); iter++)
{
cout<<*iter<<endl;
}

Program output:

void Assign (Const_iterator first,const_iterator last); Resets the list of [first,last] elements in the list to the current element

such as: List<double> A (5,10.1);
List<double> b (3,5.1);
A.assign (B.begin (), B.end ());
List<double>::iterator ITER;
For (Iter=a.begin (); Iter!=a.end (); iter++)
{
cout<<*iter<<endl;
}

Program output:

Iterator Insert (iterator it,const t& x); Adds an element x to an iterator in the list before it points to an element

such as: List<double> A (5,10.1);
A.insert (++a.begin (), 5.1); here can not use A.begin () +1, will error, the reason has not found information
List<double>::iterator ITER;
For (Iter=a.begin (); Iter!=a.end (); iter++)
{
cout<<*iter<<endl;
}

Program output:

Iterator Insert (iterator it,int n,const t& x); Adds n identical elements to an iterator in the list before it points to an element x

such as: List<double> A (5,10.1);

A.insert (++a.begin (), 2,5.1); here can not use A.begin () +1, will error, the reason has not found information
List<double>::iterator ITER;
For (Iter=a.begin (); Iter!=a.end (); iter++)
{
cout<<*iter<<endl;
}

Program output:

Iterator Insert (iterator it,const_iterator first,const_iterator last); Data in the list between [First,last] that the iterator points to an element before inserting another same type list

such as: List<double> A (5,10.1);
List<double> b (2,5.1);
A.insert (++a.begin (), B.begin (), B.end ());
List<double>::iterator ITER;
For (Iter=a.begin (); Iter!=a.end (); iter++)
{
cout<<*iter<<endl;
}

Program output:

3) Delete function

  Iterator Erase (iterator it); Removes the iterator that points to the element in the list;

Iterator Erase (iterator first,iterator last); Delete the element in the list [First,last], and note that last is the next element that points to the final element to be deleted;

void Pop_front (); Delete the first element of a list, the list cannot be empty

void Pop_back (); Delete the last element of the list, the list cannot be empty

void Clear (); Clears all the elements in the list, functionally equivalent to erase (List.begin (), List.end ());

void Remove (const t& x); Remove all elements of the list that have the value X

void Remove_if (Pred PR); Removes all elements in the list that make PR (x) ==true

void Unique (); Delete adjacent repeating elements in the list, and then go back to the general sort

void unique (Pred PR); Use PR to determine whether to delete an element

4) Traversal function

  Reference Front (); Returns a reference to the first element

Reference back (); Returns a reference to the tail element

Iterator begin (); Returns the list header pointer, pointing to the first element

Iterator End (); Returns the end of a list pointer to the next position of the last element in the list

Reverse_iterator Rbegin (); Reverse iterator, pointing to the last element

Reverse_iterator rend (); A reverse iterator that points to the position before the first element

5) Judgment function

  BOOL Empty () Const: Determines whether the list is empty and returns true if NULL

6) Size function

  void Resize (size_type n); Adjust the elements of the list to N, multiple deletions, and a random value

void Resize (size_type n,t& x); Adjusts the elements of the list to N, multiple deletions, and a value of X

int size () const; Returns the number of elements in the current list

int max_size () const; Returns the maximum allowable list element number value

7) Other functions

  void swap (list& x); Swapping elements from two lists

void Splice (iterator it,list& x); Link the list x to the current list iterator it points to the location where the list x becomes empty

void Splice (iterator it,list& x,iterator first); Links the elements of the first position of the list x to the location where the current list iterator it points, and the element to which the first point is removed in list x

void Splice (iterator it,list& x,iterator first,iterator last); Link the element of the [first,last] position of the list X to the current list iterator it points to, and delete the interval in list x [First,last]

void merge (list& x); Merges two linked lists and makes them default ascending

void merge (list& x,pred PR); Merge two linked lists, sorted according to comparison function PR () decision

void sort (); Sort linked list, default ascending

void sort (Pred PR); Sort the list, sort according to the comparison function PR () decision

void reverse (); Invert list elements

STL Standard Library of C + + learning Notes (iv) 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.