cppreference.com -> C++ Lists -> Details

C++ Lists


assign

Syntax:

  void assign( input_iterator start, input_iterator end );
  void assign( size_type num, const TYPE &val );

The assign() function either sets the list to the sequence start to end, or assigns the list num elements of type val.

Related topics:
insert(),

back

Syntax:

  reference back();

The function back() returns a reference to the last element of the list.

Related topics:
front(), pop_back(),

begin

Syntax:

  iterator begin();

The function begin() returns an iterator to the first element of the list. For example,

    // Create a list of characters
    list<char> charList;
    for( int i=0; i < 10; i++ )
      charList.push_front( i + 65 );

    // Display the list
    list<char>::iterator theIterator;
    for( theIterator = charList.begin(); theIterator != charList.end(); theIterator++ )
      cout << *theIterator;

Related topics:
end(),

clear

Syntax:

  void clear();

The clear() function deletes all elements from the list.


empty

Syntax:

  bool empty();

The function empty() returns true if the list is empty, and false otherwise. For example:

    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();
    }
    
    

end

Syntax:

  iterator end();

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

Related topics:
begin(),

erase

Syntax:

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

The erase() function removes the element at location pos, or removes the elements between start and end. The return value is an iterator to the element after the last element that we removed.


front

Syntax:

  reference front();

The function front() 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();
    }
Related topics:
back(),

get_allocator

Syntax:

  allocator_type get_allocator();

The function get_allocator() returns the allocator for the list.


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 );

The insert() function either inserts val before the element at pos, inserts num copies of val before the element at pos, or inserts the sequence denoted by start and end before the element at pos. The return value is an iterator to the inserted element.


max_size

Syntax:

  size_type max_size();

The function max_size() returns the maximum number of elements that the list can hold.


merge

Syntax:

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

The function merge() merges the list with lst, producing a combined lists that is in order. If compfunction is specified, then it is used as the comparison function for the lists.


pop_back

Syntax:

  void pop_back();

The pop_back() function removes the last element of the list.

Related topics:
pop_front(),

pop_front

Syntax:

  void pop_front();

The function pop_front() removes the first element of the list.

Related topics:
pop_back(),

push_back

Syntax:

  void push_back( const TYPE &val );

The push_back() function appends val to the end of the list. For example:

    list<int> the_list;
    for( int i = 0; i < 10; i++ )
      the_list.push_back( i );
Related topics:
push_front(),

push_front

Syntax:

  void push_front( const TYPE &val );

The push_front() function inserts val at the beginning of the list.

Related topics:
push_back(),

rbegin

Syntax:

  reverse_iterator rbegin();

The function rbegin() returns a reverse iterator to the end of the list.

Related topics:
rend(),

remove

Syntax:

  void remove( const TYPE &val );

The function remove() removes all elements that are equal to val from the list. Fpr example,

    // Create a list that has the first 10 letters of the alphabet
    list<char> charList;
    for( int i=0; i < 10; i++ )
      charList.push_front( i + 65 );

    // Remove all instances of 'E'
    charList.remove( 'E' );
  


remove_if

Syntax:

  void remove_if( UnPred pr );

The remove_if() function removes all elements from the list for which the unary predicate pr is true.


rend

Syntax:

  reverse_iterator rend();

The function rend() returns a reverse iterator to the beginning of the list.


resize

Syntax:

  void resize( size_type num, TYPE val );

The function resize() changes the size of the list to num. Any elements that are added as padding will have the value val.


reverse

Syntax:

  void reverse();

The function reverse() reverses the list.


size

Syntax:

  size_type size();

The size() function returns the number of elements in the list.


sort

Syntax:

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

The sort() function sorts the lists, optionally using compfunction as the comparison function to determine if an element is less than another.


splice

Syntax:

  void splice( iterator pos, list &lst );
  void splice( iterator pos, list &lst, iterator del );
  void splice( iterator pos, list &lst, iterator start, iterator end );

The splice() function inserts lst at location pos. If specified, the element(s) at del or from start to end are removed.


swap

Syntax:

  void swap( list &lst );

The swap() function exchanges the elements in lst with those in the current list.


unique

Syntax:

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

The function unique() removes all duplicate elements from the list. If pr is specified, then it is used to determine uniqueness.