list (list) Common member functions

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Encountered a function list commonly used title in the brush process, so conclude, I hope useful.

list the header file:

#include <list>
using namespace std;

list storage method using the chain, so the list can not be a random access, but the list efficient insert and delete elements.

list of common interfaces:

Sentenced empty: empty
Syntax:

 bool empty();  

empty () function returns true (true) if the list is empty, false otherwise. E.g:

list<int> the_list;    
for( int i = 0; i < 10; i++ )        
the_list.push_back( i );       
 while( !the_list.empty() ) {        
 cout << the_list.front() << endl;        	
 the_list.pop_front();      
 }  

Gets the number of elements in list size
syntax:

  size_type size();  

size () function returns the number of list elements.

增 push_back push_front insert

Inserts an element at the tail of the list: push_back
push_back

syntax:

 void push_back( const TYPE &val );  
push_back()将val连接到链表的最后。例如:

list<int> the_list;
       for( int i = 0; i < 10; i++ ){
             the_list.push_back( i );

}

In the list header insert elements: push_front
push_front

syntax:

 void push_front( const TYPE &val );  
push_front()函数将val连接到链表的头部。

Inserts an element at the specified position in the list: INSERT
INSERT

syntax:

 iterator insert( iterator pos, const TYPE &val );  
   void insert( iterator pos, size_type num, const TYPE &val );   
    void insert( iterator pos, input_iterator start, input_iterator end );  

insert () is inserted into the position pos val element or elements before pos val num inserted into, or inserted into the element start to end between the pos position. The return value is an iterator to the inserted elements.

删 pop_back pop_front erase clear

Delete the last element of the list, but not the return value of the last element of the list: pop_back

pop_back
syntax:

void pop_back();  
pop_back()函数删除链表的最后一个元素。

Delete list element header, but does not return the first element of the list: pop_front
pop_front
syntax:

void pop_front();  
pop_front()函数删除链表的第一个元素。

Elements on the list to delete the specified location: erase

erase
syntax:

 iterator erase( iterator pos );   
  iterator erase( iterator start, iterator end );  

erase () function to delete the indicated position pos elements, removing elements between the start and end. The return value is an iterator to the next element of the last element to be removed.

Clear all the elements of the list of deleted

clear
Syntax:

 void clear();  
clear()函数删除list的所有元素。

Gets the last element of the first element of the list back and front

Gets the last element of the list, but does not delete the last element:
the Back
syntax:

 reference back();  
back()函数返回一个引用,指向list的最后一个元素。

Get the first element of the list, but does not delete the first element:
Front
syntax:

reference front();  

front () function returns a reference to the first element of the list.

list<int> the_list;      
for( int i = 0; i < 10; i++ )        
the_list.push_back( i );        
while( !the_list.empty() ) {        
cout << the_list.front() << endl;        the_list.pop_front();      }  

Traversing the list begin end
get an iterator pointing to the first element of the list:

begin
syntax:

iterator begin();  

begin () function returns an iterator to the first element of the list. E.g,

// 创建一个元素类型是字符的链表      
list<char> charList;      
for( int i=0; i < 10; i++ )        
charList.push_front( i + 65 );  
// 显示这个链表     
 list<char>::iterator theIterator;      
 for( theIterator = charList.begin();
  theIterator != charList.end(); 
  theIterator++ )        
  cout << *theIterator;

Get to the end of the list iterator:
End

syntax:

  iterator end();  

end () function returns an iterator to the end of the list.

List merge Merge
Merge
syntax:

 void merge( list &lst );    
 void merge( list &lst, Comp compfunction );  

merge () function and connected together lst own list, generates a combination list neatly arranged. If you specify compfunction, then specify a function as a basis for comparison.

The elements of the list in reverse order Reverse
Reverse
syntax:

  void reverse();  

reverse () function to list all the elements reversed.

Of the elements of the list to sort sort

Sort (sort)
syntax:

void sort();    
void sort( Comp compfunction );  

sort () function to sort the list, the default is ascending. If the specified compfunction, then it is determined using the specified function of the size of two elements.

Modifying the length of the list resize

The size of the modified list, val filled with excess elements: resize

resize
syntax:

 void resize( size_type num, TYPE val );  

a resize () function to change the size of the list num. Was added extra elements are assigned the value val

Delete the list of repeating elements UNIQUE
UNIQUE
syntax:

void unique();   
 void unique( BinPred pr );  

unique () function to delete the list of all duplicate elements. If you specify pr, pr is used to determine whether to delete

Guess you like

Origin blog.csdn.net/weixin_42187898/article/details/93501646