2.2 Sequential Container-list

Source: Internet
Author: User

List (doubly linked list)

1)

*: Contains header file list

* *: Does not support random access; Adding or deleting elements time is a constant, only need to modify the pointer

2) member functions

*: the member function of vector list basically has

* *: The following are some unique member functions

The sort () algorithm requires random access, so the list is not supported, so introduce a member function sort ()

3) List Example

*

//examples of common member functions#include <iostream>#include<list>#include<algorithm>using namespacestd;classa{Private:        intN;  Public: A (intn_) {N=N_; } friendBOOL operator< (ConstA & A,ConstA &A2); FriendBOOL operator==(ConstA & A,ConstA &A2); Friend Ostream&operator<< (Ostream & O,ConstA &a2);};BOOL operator< (ConstA & A,ConstA &A2) {    returna.n<A2.N;}BOOL operator==(ConstA & A,ConstA &A2) {    returna.n==A2.N;} Ostream&operator<< (Ostream & O,ConstA &a) {o<<A.N; returno;} Template<classT>voidPrint (T first,t last) { for(; first!=last;++First ) cout<<*first<<" "; cout<<Endl;}intMain () {A a[5]={1,3,2,4,2}; A b[7]={Ten, -, -, -, -, +, +}; List<a>lst1 (a,a+5), Lst2 (b,b+7);  Lst1.sort (); //sort () Here is the member function, not the algorithmcout<<"1)";    Print (Lst1.begin (), Lst1.end ()); Lst1.remove (2);//Delete and 2 equal parameterscout<<"2)";    Print (Lst1.begin (), Lst1.end ());  Lst2.pop_front (); //Delete First elementcout<<"3)";    Print (Lst2.begin (), Lst2.end ()); Lst2.unique (); //Delete and previous equal elementscout<<"4)";    Print (Lst2.begin (), Lst2.end ());    Lst2.sort (); Lst1.merge (LST2);//merge Lst2 to Lst1, and delete Lst2cout<<"5)";    Print (Lst1.begin (), Lst1.end ()); cout<<"6)";    Print (Lst2.begin (), Lst2.end ());  Lst1.reverse (); //Reverse Frontcout<<"7)";    Print (Lst1.begin (), Lst1.end ()); Lst2.insert (Lst2.begin (), a+1, A +4); List<A>:: Iterator p1,p2,p3; P1=find (Lst1.begin (), Lst1.end (), -); P2=find (Lst2.begin (), Lst2.end (),2); P3=find (Lst2.begin (), Lst2.end (),4); Lst1.splice (P1,LST2,P2,P3);//Insert [P2,P3] before P1 and remove from Lst2cout<<"8)";     Print (Lst1.begin (), Lst1.end ()); cout<<"9)";     Print (Lst2.begin (), Lst2.end ()); return 0;} 

**

//List of Joseph questions#include <iostream>#include<list>using namespacestd;intMain () {list<int>monkeys; intn,m;  while(true) {cin>>n>>m; if(n==0&&m==0) Break;    Monkeys.clear ();  for(intI=1; i<=n;++i) monkeys.push_back (i); List<int>::iterator it=Monkeys.begin ();  while(Monkeys.size () >1){         for(intI=1; i<m;++i) {            ++it; if(it==monkeys.end ()) It=Monkeys.begin (); } It=Monkeys.erase (IT); if(it==monkeys.end ()) It=Monkeys.begin (); } cout<<*it<<Endl;} return 0;}

This example is also possible with vectors, because the erase operation of the vector involves the movement of the elements, not the constant time, and the significant difference in speed when n is large.

2.2 Sequential 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.