[C ++ STL] Things of C ++ STL-List (two-way linked list)

Source: Internet
Author: User

I. Overview

List stores elements in the linked list in sequence. Compared with vectors, list allows fast insertion and deletion, but random access is slow.

A list is not only a two-way linked list, but also a ring two-way linked list.

Ii. Use

# Include <list>

Using namespace STD;

 

Note: List is a range of "pre-closed and post-open", that is

List <int> LT (); // create a linked list with 10 elements as 6

Cout <* (LT. End (); // output garbled characters

Cout <* (-- lt. End (); // output the last element 6


Iii. Main Functions

Assign () assigns a value to the list
Back () returns the last element
Begin () returns the iterator pointing to the first element.
Clear () delete all elements
Empty () returns true if list is empty.
End () returns the iterator at the end.
Erase () deletes an element.
Front () returns the first element
Get_allocator () returns the list Configurator
Insert (iteator, 10) insert an element 10 until the element iterates its iteator. Generally, iteator = find (list. Begin (), list. End (), 3)
Max_size () returns the maximum number of elements that a list can hold.
Merge (list <t> & X) combines X to * This
Pop_back () Delete the last element
Pop_front () deletes the first element
Push_back () adds an element to the end of the list.
Push_front () adds an element to the list header.
Rbegin () returns the reverse iterator pointing to the first element.
Remove () delete an element from the list
Remove_if () deletes an element based on the specified conditions.
Rend () points to the reverse iterator at the end of the list
Resize () changes the list size.
Reverse () Reverse the list element
Size () returns the number of elements in the list.
Sort () sorts the list
Splice (iterator position, list & X) // list. splice (Position, list2) // prior to position in list
Splice (iterator position, list & X, iterator I) // Insert the element before position in the list
Splice (iterator position, list & X, iterator first, iterator last) // Insert the elements between first-last to position in the list.
Swap ()
Exchange two lists
Unique ()
Delete repeated elements in the list

Iv. Example

# Include <iostream> # include <list> # include <numeric> # include <algorithm> using namespace STD; // create a list container instance listinttypedef list <int> listint; // create a listchartypedef list <char> listchar; int main (void) {listint listone; // create a listone listint: iterator I; // declare I as the iterator listone. push_front (2); // Add the data listone from the front of the List container. push_front (1); listone. push_back (3); // Add the data listone after the list container. push_back (4); c Out <"listone. Begin () --- listone. End ():" <Endl; // display data from the end for (I = listone. Begin (); I! = Listone. end (); ++ I) cout <* I <"" <Endl; // displays the data in listone from the back to the end, listint: reverse_iterator IR; cout <"listone. rbegin () --- listone. rend (): "<Endl; For (IR = listone. rbegin (); IR! = Listone. rend (); IR ++) {cout <* IR <";}cout <Endl; // use STL accumulate (accumulate) algorithm int result = accumulate (listone. begin (), listone. end (), 0); cout <"sum =" <result <Endl; cout <"------------------" <Endl; // ------------------------ // use the list container to process sorted data // ---------------------------- // use listchar to create a listone listchar listtwo object; // declare I as the listchar iterator :: iterator J; // added the data listtwo to the listtwo container. push_front ('A'); listtwo. push_front ('B'); // Add the data listtwo TO THE listtwo container. push_back ('x'); listtwo. push_back ('y'); // The cout <"listtwo. begin () --- listtwo. end (): "<Endl; For (j = listtwo. begin (); J! = Listtwo. end (); ++ J) cout <char (* j) <"; cout <Endl; // use the max_element algorithm of STL to find the maximum element in listtwo and display J = max_element (listtwo. begin (), listtwo. end (); cout <"the maximum element in listtwo is:" <char (* j) <Endl; return 0 ;}

V. Example

# Include <iostream> # include <list> using namespace STD; typedef list <int> intlist; // defines the bidirectional linked list void put_list (intlist list, const char * name) // display all elements of the list queue from the beginning to the back {intlist: iterator plist; cout <"the contents of" <name <":"; for (plist = List. begin (); plist! = List. end (); plist ++) cout <* plist <"; cout <Endl;} int main (void) {intlist list1; // The list1 object is initially empty. The intlist list2 (10, 6); // The list2 object initially has 10 elements with 6 values (list2.begin (), -- list2.end ()); // The list3 object initially has nine elements with 6 values: intlist: iterator I; // declare a bidirectional iterator named I put_list (list1, "list1 "); // put_list (list2, "list2"); put_list (list3, "list3"); list1.push _ back (2 ); // Add two elements list1.push _ back (4); cout <"list1.push _ back (2) and list1.push _ back (4):" <Endl; put_list (list1, "list1"); list1.push _ Front (5); // Add two elements list1.push _ Front (7) from the front of the list1 series ); cout <"list1.push _ Front (5) andlist1.push _ Front (7):" <Endl; put_list (list1, "list1"); list1.insert (++ list1.begin (), ); // insert 3 9 cout data in the middle of the list1 sequence <"list1.insert (list1.begin () +, 9):" <Endl; put_list (list1, "list1"); cout <"list1.front () =" <list1.front () <Endl; // test the reference class function cout <"list1.back () = "<list1.back () <Endl; list1.pop _ Front (); // remove an element list1.pop _ back () from the beginning and end of the list1 sequence (); cout <"list1.pop _ Front () and list1.pop _ back ():" <Endl; put_list (list1, "list1"); list1.erase (++ list1.begin ()); // clear the 2nd cout elements in list1 <"list1.erase (++ list1.begin ():" <Endl; put_list (list1, "list1"); list2.assign ); // assign a value to list2 and display cout <"list2.assign (8, 1):" <Endl; put_list (list2, "list2"); cout <"list1.max _ SIZE (): "<list1.max _ SIZE () <Endl; // display the status information of the sequence cout <" list1.size (): "<list1.size () <Endl; cout <"list1.empty ():" <list1.empty () <Endl; put_list (list1, "list1"); // put_list (list3, "list3"); cout <"list1> list3:" <(list1> list3) <Endl; cout <"list1 <list3: "<(list1 <list3) <Endl; list1.sort (); // sort the put_list (list1," list1 "); list1.splice (++ list1.begin (), list3); // combine list3 into list1 + + list1.begin () put_list (list1, "list1"); put_list (list3, "list3"); Return 0 ;}

Output:

The contents oflist1 : The contents oflist2 : 6 6 6 6 6 6 6 6 6 6 The contents oflist3 : 6 6 6 6 6 6 6 6 6 list1.push_back(2) and list1.push_back(4):The contents oflist1 : 2 4 list1.push_front(5) andlist1.push_front(7):The contents oflist1 : 7 5 2 4 list1.insert(list1.begin()+1,3,9):The contents oflist1 : 7 9 9 9 5 2 4 list1.front()=7list1.back()=4list1.pop_front() and list1.pop_back():The contents oflist1 : 9 9 9 5 2 list1.erase(++list1.begin()):The contents oflist1 : 9 9 5 2 list2.assign(8,1):The contents oflist2 : 1 1 1 1 1 1 1 1 list1.max_size(): 357913941list1.size(): 4list1.empty(): 0The contents oflist1 : 9 9 5 2 The contents oflist3 : 6 6 6 6 6 6 6 6 6 list1>list3: 1list1<list3: 0The contents oflist1 : 2 5 9 9 The contents oflist1 : 2 6 6 6 6 6 6 6 6 6 5 9 9 The contents oflist3 : 

Iv. Supplement: Use the STL standard function find to search for vector and list linked lists.

# Include <vector> # include <algorithm> # include <iostream> using namespace STD; Class Example {public: Example (INT Val) // constructor {I = val ;} bool operator = (example const & RHs) // operator overload {return (I = RHS. I )? True: false;} PRIVATE: int I ;}; int main (void) {vector <example> ve; ve. push_back (1); vector <example>: iterator it; example ELEM (1); It = find (VE. begin (), ve. end (), ELEM); cout <boolalpha <(* It = ELEM); // boolalpha represents true and false in the form of a symbolic return 0 ;}

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.