STL Learning Series Six: List container

Source: Internet
Author: User

Introduction to List
    • List is a doubly linked list container that efficiently inserts and deletes elements.
    • The list is not allowed to randomly access elements, so at is not supported. (POS) functions and [] operators. it++ (OK), it+5 (ERR)
    • #include <list>

Default construction of 1.list objects
    • List adopts the template class implementation, the object's default construction form:list<t> LSTT; Such as:
    • List<int> Lstint; Defines a list container that holds an int.
    • List<float> lstfloat; Defines a list container that holds float.
    • You can also set pointer types or custom types within angle brackets.
2.list Add and remove operations
    • List.push_back (Elem); Add an element at the end of the container
    • List.pop_back (); Delete the last element in a container
    • List.push_front (Elem); Inserts an element at the beginning of the container
    • List.pop_front (); Remove the first element from the beginning of the container
#include <iostream>using namespace std; #include <list>void objPlay2 () {    list<int> lstint;    Lstint.push_back (1);    Lstint.push_back (3);    Lstint.push_back (5);    Lstint.push_back (7);    Lstint.push_back (9);//This time the list is (1,3,5,7,9)    lstint.pop_front ();//delete header element (1) This time the list is (3,5,7,9)    Lstint.pop_front (); Delete Header Element (3) This time the list is (5,7,9)    Lstint.push_front (11);//Head insertion 11 This time list is (11,5,7,9)    Lstint.push_front (13); /Head Insert 13 This time list is (13,11,5,7,9)    lstint.pop_back ();//delete tail element This time list is (13,11,5,7)    lstint.pop_back ();// Delete Tail element This time the list is (13,11,5,)}int main () {    objPlay2 ();        return 0;}
3.list of data access
    • List.front (); Returns the first element.
    • List.back (); Returns the last element.
void ObjPlay3 () {    list<int> lstint;    Lstint.push_back (1);    Lstint.push_back (3);    Lstint.push_back (5);    Lstint.push_back (7);    Lstint.push_back (9);//This time the list is (1,3,5,7,9)    int ifront = Lstint.front ();    Take the head of List (1)    int iback = Lstint.back ();        Take the tail of list (9)    lstint.front () = one;            Set the value to the head of the list, overwriting the original value    lstint.back () = +;            Sets the value for the tail of the list, overwriting the original value}

4.list and Iterators
    • List.begin (); Returns an iterator to the first element in a container.
    • List.end (); Returns an iterator after the last element in the container.
    • List.rbegin (); Returns an iterator to the first element in the container.
    • List.rend (); Returns an iterator to the back of the last element in the container.
void ObjPlay4 () {    list<int> lstint;    Lstint.push_back (1);    Lstint.push_back (3);    Lstint.push_back (5);    Lstint.push_back (7);    Lstint.push_back (9);    for (List<int>::iterator it = Lstint.begin (); It! = Lstint.end (); ++it)//forward iterator    {        cout << *it << "\ t";    }    cout << Endl;    for (List<int>::reverse_iterator rit = Lstint.rbegin (); RIT! = Lstint.rend (); ++rit)//Reverse iterator    {        cout < < *rit << "\ t";    }}

Parametric construction of 5.list objects
    • List (beg,end); The constructor copies the elements in the [Beg, end] interval to itself. Note that the interval is left-closed and right-open.
    • List (N,elem); A constructor copies n Elem to itself.
    • List (const list &lst); Copy constructor.

void ObjPlay5 () {    list<int> lstinta;    Lstinta.push_back (1);    Lstinta.push_back (3);    Lstinta.push_back (5);    Lstinta.push_back (7);    Lstinta.push_back (9);    List<int> Lstintb (Lstinta.begin (), Lstinta.end ());        1 3 5 7 9    list<int> LSTINTC (5, 8);                            8 8 8 8 8     list<int> lstintd (lstinta);                        1 3 5 7 9}

Assignment of 6.list
    • List.assign (Beg,end); Assigns a copy of the data in the [Beg, end] interval to itself. Note that the interval is left-closed and right-open.
    • List.assign (N,elem); Assigns an n elem copy to itself.
    • list& operator= (const list &lst); Overloaded equals operator
    • List.swap (LST); The LST is interchanged with its own elements.
void ObjPlay6 () {    list<int> Lstinta, LSTINTB, LSTINTC, lstintd;//this place is defined by the list, notice and the difference of the structure of the argument    Lstinta.push _back (1);    Lstinta.push_back (3);    Lstinta.push_back (5);    Lstinta.push_back (7);    Lstinta.push_back (9);    Lstintb.assign (Lstinta.begin (), Lstinta.end ());        1 3 5 7 9    lstintc.assign (5, 8);                            8 8 8 8 8    lstintd = Lstinta;                            1 3 5 7 9    Lstintc.swap (LSTINTD);                        Interchangeable

Size of 7.list
    • List.size (); Returns the number of elements in a container
    • List.Empty (); Determine if the container is empty
    • List.resize (num); The container is re-specified as NUM, and if the container is longer, the new position is populated with the default value. If the container is shorter, the element at the end of the container length is removed.
    • List.resize (num, elem); Reassign the container's length to num, and if the container is longer, fill the new position with the Elem value. If the container is shorter, the element at the end of the container length is removed.
void ObjPlay7 () {    list<int> lstinta;    Lstinta.push_back (1);    Lstinta.push_back (3);    Lstinta.push_back (5);    if (!lstinta.empty ())    {        int isize = Lstinta.size ();        3        lstinta.resize (5);            1 3 5 0 0        lstinta.resize (7, 1);            1 3 5 0 0 1 1        lstinta.resize (2);            1 3    }}

Insertion of 8.list
    • List.insert (Pos,elem); Inserts a copy of the Elem element at the POS location, returning the location of the new data.
    • List.insert (Pos,n,elem); Insert n elem data at POS location, no return value.
    • List.insert (Pos,beg,end); The data in the [Beg,end] interval is inserted at the POS location, with no return value.
void ObjPlay8 () {    list<int> lsta;    List<int> Lstb;    Lsta.push_back (1);    Lsta.push_back (3);    Lsta.push_back (5);    Lsta.push_back (7);    Lsta.push_back (9);    Lstb.push_back (2);    Lstb.push_back (4);    Lstb.push_back (6);    Lstb.push_back (8);    Lsta.insert (Lsta.begin (), one by one);        {One, 1, 3, 5, 7, 9}    Lsta.insert (++lsta.begin (), 2,);        {11,33,33,1,3,5,7,9}    Lsta.insert (Lsta.begin (), Lstb.begin (), Lstb.end ());    {2,4,6,8,11,33,33,1,3,5,7,9}}

Removal of 9.list
    • List.clear (); Remove all data from a container
    • List.erase (Beg,end); Deletes the data for the [beg,end] interval, returning the position of the next data. Attention is left closed right open
    • List.erase (POS); Deletes the data at the POS location and returns the location of the next data.
    • Lst.remove (Elem); Removes all elements in the container that match the Elem value.
void ObjPlay9 () {    //1. Delete elements within the interval    list<int> lstint;    Lstint.push_back (1);    Lstint.push_back (3);    Lstint.push_back (5);    Lstint.push_back (7);    Lstint.push_back (9);    List<int>::iterator Itbegin = Lstint.begin ();    ++itbegin;//begin points to 3    list<int>::iterator itend = Lstint.begin ();    ++itend;    ++itend;    ++itend;//end Point 7    lstint.erase (Itbegin, itend);    At this point the container lstint contains three elements in 1,7,9 order.    int arr[10] = {1, 3, 2, 3, 3, 3, 4, 3, 5, 3};    for (List<int>::iterator it = Lstint.begin (); It! = Lstint.end ();)    //parentheses do not need to write  ++it    {        if (*it = = 3)        {            it = lstint.erase (it);       The iterator is the parameter, the element 3 is removed, and the next element position after the data is deleted is returned to the iterator. At this point, do not execute  ++it;          }        else        {            ++it;}    }    2. Method of removing the element equal to 3 in the container two        lstint.remove (3);    3. Delete all elements of Lstint        lstint.clear ();            Container is empty}

Reverse order of 10.list
    • Lst.reverse (); The list of reverses, such as LST, contains the 1,3,5 element, and when this method is run, LST contains the 5,3,1 element.
void ObjPlay10 () {    list<int> lstint;    Lstint.push_back (1);    Lstint.push_back (3);    Lstint.push_back (5);    Lstint.push_back (7);    Lstint.push_back (9);    Lstint.reverse ();            9 7 5 3 1}

Transfer from http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_stl_006.html

STL Learning Series Six: List container

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.