Preliminary STL Container List

Source: Internet
Author: User

List

Characteristics:

1. It is essentially a doubly linked list

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2fuz3hpyw9idxb0/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

2. Include <list> header file #include <list> when using

3. Random access iterators are not supported, only bidirectional iterators can be used//so some algorithms and operator operations are not possible

4. Insertion and deletion in any position is constant time

member functions

Initialization

List <int> intlist0;              Create an empty intlistlist <int> intlist1 (3);       Consists of 3 elements list <int> Intlist2 (5, 2);   Includes 5 elements, each of which is a 2


Access Operations

Front ()

Returns the first element (does not check if the container is empty)

Back ()

Returns the last element (does not check if the container is empty)

Pop_back ()

Add an element to the end of the list

Pop_front ()

Add an element to the linked list header

Push_back ()

Delete an element at the end of a list

Push_front ()

Delete an element of a linked list header

Assign ()

Erase the elements in the linked list and copy the new elements into the target list

Empty ()

Infers whether the linked list is empty. The assumption is null, which returns TRUE.

Example:

#include <iostream> #include <list>using namespace Std;int main () {list<int> intlist;list<int> :: Iterator Intiter;list<int>::reverse_iterator intriter;if (Intlist.empty ()) cout<< "Intlist is empty" <<   "\ n"; For empty intlist.assign (4,1);cout<< "Run Assign (4,1), intlist:"; for (Intiter=intlist.begin (); intiter!=intlist.end               (); ++intiter) cout<<*intiter;cout<< "\ n";                Output 1111cout<< "Intlist.front:"; Cout<<intlist.front ();cout<< "\ n";              Output 1cout<< "Intlist.back:"; Cout<<intlist.back ();cout<< "\ n"; Output 1intlist.push_back (5); Intlist.push_front (9);cout<< "Run Push_back and Push_front:"; for (intiter=                  Intlist.begin (); Intiter!=intlist.end (); ++intiter) cout<<*intiter;cout<< "\ n"; Output 911115cout<< "reverse output:"; for (Intriter=intlist.rbegin (); Intriter!=intlist.rend (); ++intriter) cout<<*    Intriter;                                  cout<< "\ n"; Output 511119INTLIST.POp_back (); Intlist.pop_back (); Intlist.pop_front (); Intlist.pop_front ();cout<< "Run 2 pop_back and 2 times pop_front:";                                         For (Intiter=intlist.begin (); Intiter!=intlist.end (); ++intiter) cout<<*intiter<< "";cout<< "\ n"; Output 11getchar (); return 0;}

Manipulating elements in the list

Clear ()

Remove all elements from the container

Erase (position)

Deletes an element at a location specified by position

Erase (Beg,end)

Remove all elements from beg to end-1

Insert (position, elem)

Inserts a copy of Elem into the location specified by position and returns the position of the new element

Inser (position, N, Elem)

Inserts n copies of Elem into a location specified by position

Insert (position, beg, end)

Inserts a copy of all elements from beg to end-1 into a location specified by position in Veclist

Resize (num)

Change the number of elements to Num.

Assume that size () is added. The default constructor is responsible for creating these new elements

Resize (num, elem)

Change the number of elements to Num.

Assume that size () is added. The default constructor initializes these new elements to Elem

Size ()

The current number of elements in the linked list

Some algorithms for list

Merge ()

Lst1.merge (LST2); Merge Lst2 to Lst1 and empty lst2

Sort ()

Sort linked list, default ascending

Reverse ()

Reverse linked List

Remove ()

Delete all elements of a linked list that match values

Splice ()

Two linked lists, combined with a second linked list empty

Unique ()

Delete adjacent repeating elements

#include <iostream> #include <list> #include <algorithm>using namespace Std;int main () {list<int& Gt    L (5,2);    List<int>:: Iterator pl;    L.unique ();   for (auto I=l.begin (); I!=l.end (); i++) cout<<*i;    Output 2 cout<<endl;    List<int> l1{1,3,2,5,4};   for (auto I=l1.begin (); I!=l1.end (); i++) cout<<*i;    Output 13254 cout<<endl;    L1.sort ();   for (auto I=l1.begin (); I!=l1.end (); i++) cout<<*i;    Output 12345 cout<<endl;    L1.reverse ();   for (auto I=l1.begin (); I!=l1.end (); i++) cout<<*i;    Output 54321 cout<<endl;    L1.push_back (3);   for (auto I=l1.begin (); I!=l1.end (); i++) cout<<*i;    Output 543213 cout<<endl;     L1.remove (3);   for (auto I=l1.begin (); I!=l1.end (); i++) cout<<*i;    Output 5421 cout<<endl;    List<int> L2 (1);      L2.merge (L1);   For (Pl=l2.begin ();p l!=l2.end ();p l++) cout<<*pl; Output 05421 COUT&LT;&LT;endl; List<int> l3{1,2,3,4,5,6,7,8};  Suppose you want to cut the 1234 in the L2 between 05 auto p1 = Find (L3.begin (), L3.end (), 1);    The interception process contains left not containing right auto P2 = find (L3.begin (), L3.end (), 5);  Auto P3 = Find (L2.begin (), L2.end (), 5);    A l2.splice (P3,L3,P1,P2) will be moved back to the very next element of the position;   For (Pl=l2.begin ();p l!=l2.end ();p l++) cout<<*pl;    Output 012345421 cout<<endl;   For (Pl=l3.begin ();p l!=l3.end ();p l++) cout<<*pl;    Output 5678 cout<<endl; return 0;}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Preliminary STL Container 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.