C + + language STL container list summary _c language

Source: Internet
Author: User

When using a std::list<> linked list, it is inevitable that the data will be added to the delete operation. There are two ways to traverse a list: Through index access, as with arrays; access through the std::list<>::iterator linked list traversal

The list in the STL is a two-way linked list, which can be inserted efficiently to delete elements.

The list does not support random access. So there is no at (POS) and operator[].

The list object List1, List2 has element List1 (1,2,3), List2 (4,5,6) respectively. list< Int>::iterator it;

Construction, deconstruction

List<elem> c//Create an empty list 

list<elem> C1 (C2)//Copy list 

list<elem>c (n) of another element of the same type Creates a list of n elements, each of which is determined by the default constructor 

list<elem>c (N,elem)///Creating a list of n elements, each of which has a value of Elem 

list<elem>c (Begin, END)///The iterator creates the list, and the iteration interval is [begin,end) 

c.~list ();       Destroy all elements, freeing up memory 

### Other ###

C.size ()///Returns the number of elements in the container 

c.swap (C2)///convert C2 and C elements to 

c.empty ()///To determine whether the container is empty 

c.max_size ()//return the maximum amount 

of data in the container C.resize (num)//re-specify the length of the linked list 

c.reverse ()//Reverse link List 

c.sort ()//To sort the list, by default ascending order, you can customize the callback function 
//Sample 
List Object L1 ( 4,3,5,1,4) 
l1.sort ();         L1 (1,3,4,4,5) 
L1.sort (Greater <int > ());//L1 (5,4,4,3,1) 

c.merge ()//() merge two ordered lists order 
//Example // 
Ascending 
list1.merge (LIST2);//List1 (1,2,3,4,5,6) list2 now empty 
//Descending 
L1 (3,2,1), L2 (6,5,4) 
L1.merge (L2, Greater <int > ());
List1 (6,5,4,3,2,1) List2 is now empty 

c.splice () 
///Two linked list (three overloaded functions) after the combination of the second list empty 


//Example 
List1.splice (+ + List1.begin (), list2); 
List1 (1,4,5,6,2,3) list2 is empty 
 list1.splice (++list1.begin (), List2,list2.begin ()); 
List1 (1,4,2,3); List2 (5,6) 
List1.splice (++list1.begin (), List2,++list2.begin (), List2.end ()); 
List1 (1, 5, 6, 2,3); List2 (4) 

### Assignment ###

C.assign (begin,end)//Assign the data in the [begin,end) interval to C 

c.assign (N,elem)////Assign the copy of N Elem to C 

c.swap (C2)///swap elements of C2 and C 

### Data Access ###

C.front ()///Returns the first Data C.back ()//returns 

the last data 

c.begin ()//Returns the iterator (pointer) to the first element 

c.end ()//returns the iterator that points to the next position in the last data ( Pointer) 

c.rbegin () 
//Returns the first data of the reverse queue, that is, the iterator c.rend ()///Returning the last of the first elements in the container 
, which returns the iterator for the next position in the final data of the reverse queue, 
//Is the iterator that returns the last element in the container 

### Insert Data ###

C.push_back (Elem)//list element Tail adds an element x 

C.push_front (Elem)//list element first element Money add an element x 

C.insert (Pos,elem)// Inserts a Elem copy at the POS position, returns the position of the new data 

C.insert (Pos,n,elem)////Insert n Elem data at POS position, no return value 

C.insert (pos,begin,end)
/ /Inserts the data in the [Begin,end) interval at the POS position, no return value 

### Delete Data ###

C.pop_back ()///delete container tail element when and only if container is not empty  

c.pop_front ()///delete container first element, if and only if container is not empty  

c.remove (Elem)//delete element  

value equal to X in container /** 
 remove_if () deletes the element that satisfies the condition (traversing the list) 
/  
void Remove_if_test () {  
showlist (g_list1);  
G_list1.remove_if (myfun);  
Showlist (G_list1);  
}  

C.clear ()///delete all elements in the container  

c.erase (POS)//delete pos position data, return next data position  

c.erase (begin,end) 
//delete [begin,end) Interval data, returns the position of the next data  

c.unique ()///delete adjacent repeating elements  
//Samples  
L1 (1, 1, 4,3,5,1)  
l1.unique ();     L1 (1,4,3,5,1)  

### Sample ###

#include <iostream> #include <stdio.h> #include <list> using namespace std; 
List < int > g_list1; 
List < int > g_list2; 
/** initializes the global list */void Initlist () {//push_back () adds one element to the tail g_list1.push_back (1); 
G_list1.push_back (2); 
G_list1.push_back (3); 
Push_front () Adds an element to the header G_list2.push_front (6); 
G_list2.push_front (5); 
G_list2.push_front (4); /** output a list/void Showlist (List < int >& listtemp) {//Size () returns the number of elements in the list cout << listtemp.size () & 

lt;< Endl;  for (List < int >:: iterator it = Listtemp.begin (); it!= listtemp.end (); + + it) {cout << * it << ' 
; 
} cout << Endl; 
}/** constructor, empty list */void Constructor_test0 () {List < int > listtemp; 
cout << listtemp.size () << Endl; 
 /** constructor, create a list containing three elements with default values of 0/void Constructor_test1 () {List < int > listtemp (3); 
Showlist (listtemp); /** constructor, build a list of five elements with a value of 1 */void Constructor_test2 () {LisT < int > listtemp (5, 1); 
Showlist (listtemp); 
 /** constructor, build a g_list1 copy list/void Constructor_test3 () {List < int > listtemp (g_list1); 
Showlist (listtemp); /** constructor, Listtemp contains g_list1 element [_first, _last)/void Constructor_test4 () {List < int > listtemp (g_list1. 
 Begin (), G_list1.end ()); 
Showlist (listtemp); /** assign () assigned value with two overloads template <class inputiterator> void assign (Inputiterator first, Inputiterator last 
 ); 
void Assign (size_type n, const t& u); 
 */void Assign_test () {List < int > listtemp (5, 1); 
 Showlist (listtemp); 
 Listtemp.assign (4, 3); 

 Showlist (listtemp); 
 Listtemp.assign (+ + g_list1.begin (), G_list1.end ()); 
Showlist (listtemp); 
 }/** operator= */void Operator_equality_test () {g_list1 = G_list2; 
 Showlist (G_LIST1); 
Showlist (G_LIST2); 
/** Front () returns the reference to the first element/void Front_test7 () {cout << g_list1.front () << Endl; /** back () returns the reference to the last element * * void BACK_test () {cout << g_list1.back () << Endl; 
 /** begin () returns the pointer to the first element (iterator)/void Begin_test () {List < int >:: Iterator it1 = G_list1.begin (); 

 cout << *++ it1 << Endl; 
 List < int >:: Const_iterator it2 = G_list1.begin (); 
 It2 + +; (*it2) + +; 

*it2 for const without modifying cout << * it2 << Endl; /** End () returns a pointer to the next position of the last element (list is null end () = Begin ())/void End_test () {List < int >:: iterator it = G_li St1.end (); 
Note: A pointer to the next position of the last element-it; 
cout << * it << Endl; 
 /** Rbegin () returns the back pointer to the last element of the list/void Rbegin_test () {List < int >:: reverse_iterator it = G_list1.rbegin (); 
for (; it!= g_list1.rend (); + + it) {cout << * it <<; 
} cout << Endl; 
/** rend () returns the back pointer to the next position in the first element of the list/void Rend_test () {List < int >:: reverse_iterator it = g_list1.rend (); 
--it; 
cout << * it << Endl; /** push_back () adds one element to the end of the chain/void Push_back_test () {SHowlist (G_LIST1); 
G_list1.push_back (4); 
Showlist (G_LIST1); 
/** Push_front () adds one element to the header/void Push_front_test () {showlist (g_list1); 
G_list1.push_front (4); 
Showlist (G_LIST1); 
/** Pop_back () deletes an element at the end of the list/void Pop_back_test () {showlist (g_list1); 

cout << Endl; 
G_list1.pop_back (); 

Showlist (G_LIST1); 
/** Pop_front () deletes one element of the header of the chain/void Pop_front_test () {showlist (g_list1); 

cout << Endl; 
G_list1.pop_front (); 
Showlist (G_LIST1); 
/** Clear () deletes all elements */void Clear_test () {showlist (g_list1); 
G_list1.clear (); 
Showlist (G_LIST1); 
/** Erase () deletes an element or an element of an area (two overloaded functions)/void Erase_test () {showlist (g_list1); 
G_list1.erase (G_list1.begin ()); 

Showlist (G_LIST1); 

cout << Endl; 
Showlist (G_LIST2); 
G_list2.erase (+ + g_list2.begin (), G_list2.end ()); 
Showlist (G_LIST2); 
/** Remove () deletes the element matching the value in the list (all matching elements are deleted)/void Remove_test () {showlist (g_list1); 
G_list1.push_back (1); 

Showlist (G_LIST1); 
G_list1.remove (1); Showlist (g_LIST1); 
BOOL Myfun (const int & value) {return (value < 2);} 
/** remove_if () deletes the element that satisfies the condition (traversing the list)/void Remove_if_test () {showlist (g_list1); 
G_list1.remove_if (Myfun); 
Showlist (G_LIST1); 
/** empty () determines whether the linked list is empty/void Empty_test () {List < int > listtemp; 
if (Listtemp.empty ()) cout << "Listtemp is empty" << Endl; 
else cout << "listtemp not Empty" << Endl; /** max_size () returns the maximum possible length of the list: 1073741823/void Max_size_test () {List < int >:: size_type nmax = G_list1.max_siz 
E (); 
cout << Nmax << Endl; 
/** Resize () redefine the chain list length (two overloaded functions): */void Resize_test () {showlist (g_list1); G_list1.resize (9); 
Fill showlist with default value (G_LIST1); 

cout << Endl; 
Showlist (G_LIST2); G_list2.resize (9, 51); 
Filling showlist (G_LIST2) with the specified value; 
/** reverse () Reverse link list/void Reverse_test () {showlist (g_list1); 
G_list1.reverse (); 
Showlist (G_LIST1); /** sort () is sorted by list, default ascending (two overloaded functions)/void Sort_test () {List < int > listtemp;
Listtemp.push_back (9); 
Listtemp.push_back (3); 
Listtemp.push_back (5); 
Listtemp.push_back (1); 
Listtemp.push_back (4); 

Listtemp.push_back (3); 
Showlist (listtemp); 
Listtemp.sort (); 

Showlist (listtemp); 
Listtemp.sort (Greater < int > ()); 
Showlist (listtemp); 
/** merge () merges two ascending sequential lists and makes them another ascending order. 
*/void Merge_test1 () {List < int > listTemp2; 
Listtemp2.push_back (3); 

Listtemp2.push_back (4); 
List < int > ListTemp3; 
Listtemp3.push_back (9); 

Listtemp3.push_back (10); 
Showlist (LISTTEMP2); 
cout << Endl; 
Showlist (ListTemp3); 

cout << Endl; 
Listtemp2.merge (ListTemp3); 
Showlist (LISTTEMP2); 
BOOL mycmp (INT-A-second) {return (int (a) > int (second));} 
/** merge () merges two descending lists and makes them another descending order. 
*/void Merge_test2 () {List < int > listTemp2; 
Listtemp2.push_back (4); 

Listtemp2.push_back (3); 
List < int > ListTemp3; 
Listtemp3.push_back (10); 

Listtemp3.push_back (9); 
Showlist (LISTTEMP2); CoUT << Endl; 
Showlist (ListTemp3); 

cout << Endl; Listtemp2.merge (ListTemp3, greater<int> ()); 
The second parameter can be defined by its own function as follows Listtemp2.merge (ListTemp3, mycmp); 
Showlist (LISTTEMP2); 
  /** Splice () combines two linked lists (three overloaded functions), and the second list clears void splice (iterator position, list<t,allocator>& x); 
  void Splice (iterator position, list<t,allocator>& X, iterator i); 
void Splice (iterator position, list<t,allocator>& x, iterator-I, iterator last); 
*/void Splice_test () {List < int > listTemp1 (g_list1); 

List < int > listTemp2 (g_list2); 
Showlist (LISTTEMP1); 
Showlist (LISTTEMP2); 

cout << Endl; 
Listtemp1.splice (+ + listtemp1.begin (), LISTTEMP2); 
Showlist (LISTTEMP1); 

Showlist (LISTTEMP2); 
Listtemp1.assign (G_list1.begin (), G_list1.end ()); 
Listtemp2.assign (G_list2.begin (), G_list2.end ()); 
Listtemp1.splice (+ + listtemp1.begin (), LISTTEMP2, + + listtemp2.begin ()); 
Showlist (LISTTEMP1); Showlist (LISTTEMP2);

Listtemp1.assign (G_list1.begin (), G_list1.end ()); 
Listtemp2.assign (G_list2.begin (), G_list2.end ()); 
Listtemp1.splice (+ + listtemp1.begin (), LISTTEMP2, + + Listtemp2.begin (), Listtemp2.end ()); 
Showlist (LISTTEMP1); 

Showlist (LISTTEMP2); 
  /** Insert () inserts one or more elements (three overloaded functions) at the specified location iterator insert (iterator position, const t& x); 
  void Insert (iterator position, Size_type N, const t& x); 
Template <class inputiterator> void Insert (iterator position, inputiterator-I, inputiterator last); 
*/void Insert_test () {List < int > listTemp1 (g_list1); 
Showlist (LISTTEMP1); 
Listtemp1.insert (Listtemp1.begin (), 51); 
Showlist (LISTTEMP1); 

cout << Endl; 
List < int > listTemp2 (g_list1); 
Showlist (LISTTEMP2); 
Listtemp2.insert (Listtemp2.begin (), 9, 51); 
Showlist (LISTTEMP2); 

cout << Endl; 
List < int > ListTemp3 (g_list1); 
Showlist (ListTemp3); 
Listtemp3.insert (Listtemp3.begin (), G_list2.begin (), G_list2.end ()); Showlist (liStTemp3); 
/** swap () swap two lists (two overloads)/void Swap_test () {showlist (g_list1); 
Showlist (G_LIST2); 

cout << Endl; 
G_list1.swap (G_LIST2); 
Showlist (G_LIST1); 
Showlist (G_LIST2); 
BOOL Same_integral_part (double, double second) {return (int (a) = Int (second));} 
/** Unique () deletes the adjacent repeating element/void Unique_test () {List < int > listtemp; 
Listtemp.push_back (1); 
Listtemp.push_back (1); 
Listtemp.push_back (4); 
Listtemp.push_back (3); 
Listtemp.push_back (5); 
Listtemp.push_back (1); 

List < int > listTemp2 (listtemp); 
Showlist (listtemp); Listtemp.unique (); 
Does not delete nonadjacent identical element showlist (listtemp); 

cout << Endl; 
Listtemp.sort (); 
Showlist (listtemp); 
Listtemp.unique (); 
Showlist (listtemp); 

cout << Endl; 
Listtemp2.sort (); 
Showlist (LISTTEMP2); 
Listtemp2.unique (Same_integral_part); 

Showlist (LISTTEMP2); 
 /** main function, list test/int main () {initlist (); 
 Showlist (G_LIST1); 

 Showlist (G_LIST2); 
 Constructor_test0 (); ConstruCtor_test1 (); 
 Constructor_test2 (); 
 Constructor_test3 (); 
 Constructor_test4 (); 
 Assign_test (); 
 Operator_equality_test (); 
 Front_test7 (); 
 Back_test (); 
 Begin_test (); 
 End_test (); 
 Rbegin_test (); 
 Rend_test (); 
 Push_back_test (); 
 Push_front_test (); 
 Pop_back_test (); 
 Pop_front_test (); 
 Clear_test (); 
 Erase_test (); 
 Remove_test (); 
 Remove_if_test (); 
 Empty_test (); 
 Max_size_test (); 
 Resize_test (); 
 Reverse_test (); 
 Sort_test (); 
 Merge_test1 (); 
 Merge_test2 (); 
 Splice_test (); 
 Insert_test (); 
 Swap_test (); 
Unique_test (); 
return 0; 

 }

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.