C + + list usage (collation) __c++

Source: Internet
Author: User

Lists stores elements sequentially in a linked list. It allows for fast insertions and deletions compared to vectors (vectors), but random access is slower.


Assign () Assign value to list
Back () returns the last element
Begin () returns the iterator that points to the first element
Clear () Delete all elements
Empty () returns True if the list is empty
End () return iterator
Erase () deletes an element
Front () returns the first element
Get_allocator () returns the Configurator of the list
Insert () Inserts an element into the list
Max_size () returns the maximum number of elements the list can hold
Merge () merges two list
Pop_back () deletes the last element
Pop_front () deletes the first element
Push_back () Add an element at the end of the list
Push_front () Add an element to the head of the list
Rbegin () returns a reverse iterator that points to the first element
Remove () delete element from list
Remove_if () deletes elements by specified criteria
Rend () a reverse iterator that points to the end of the list
Resize () Change the size of the list
Reverse () reverse the elements of the list
Size () returns the number of elements in the list
Sort () sorting list
Splice () Merge two list
Swap () swap two list
Unique () Delete duplicate elements in the list


Example one:

[CPP]  View plain  copy #include  <iostream>    #include  <list>     #include  <numeric>    #include  <algorithm>    using namespace std;      //Create an instance of a list container listint    typedef  list<int> LISTINT;   //Create an instance of a list container listchar    typedef  list<int> listchar;       void main ()     {         //use list container to process integer data          // Use Listint to create a list object named Listone         LISTINT listOne;        //declaration I for iterators         LISTINT::iterator i;                //adds data to the Listone container in the past         listone.push_front  (2);        listOne.push_front  (1);                //Add data from the back to the Listone container         listOne.push_back  (3);        listone.push_back   (4);               //display data from Listone in front of back         cout<< "Listone.begin ()--- listone.end ():" <<endl;         for  (I = listone.begin (); i !=  Listone.end ()  ++i)             cout <<  *i <<  " ";        cout << endl;                //to display data from Listone in the back of the post          LISTINT::reverse_iterator ir;        cout<< " Listone.rbegin ()---listone.rend (): <<endl;        for  (ir = Listone.rbegin ()  ir!=listone.rend (); ir++)  {             cout << *ir <<  " ";         }        cout << endl;                //using STL accumulate (cumulative) algorithm          Int result = accumulate (Listone.begin (),  listone.end (), 0);         cout<< "sum=" <<result<<endl;        cout< < "------------------" <<endl;                //--------------------------        //processing character data with list container          //--------------------------                //use Listchar to create a list object named Listone         LISTCHAR listTwo;    

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.