C++ Lists
Syntax:
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(),
Syntax:
The function back() returns a reference to the last element of the list.
Related topics:
front(),
pop_back(),
Syntax:
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(),
Syntax:
The clear() function deletes all elements from the list.
Syntax:
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();
}
Syntax:
The end() function returns an iterator the the end of the list.
Related topics:
begin(),
Syntax:
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.
Syntax:
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(),
Syntax:
allocator_type get_allocator();
|
The function get_allocator() returns the allocator for the list.
Syntax:
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.
Syntax:
The function max_size() returns the maximum number of elements that the list can hold.
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.
Syntax:
The pop_back() function removes the last element of the list.
Related topics:
pop_front(),
Syntax:
The function pop_front() removes the first element of the list.
Related topics:
pop_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(),
Syntax:
void push_front( const TYPE &val );
|
The push_front() function inserts val at the beginning of the list.
Related topics:
push_back(),
Syntax:
The function rbegin() returns a reverse iterator to the end of the list.
Related topics:
rend(),
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' );
Syntax:
void remove_if( UnPred pr );
|
The remove_if() function removes all elements from the list for which the unary predicate
pr is true.
Syntax:
The function rend() returns a reverse iterator to the beginning of the list.
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.
Syntax:
The function reverse() reverses the list.
Syntax:
The size() function returns the number of elements in the list.
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.
Syntax:
The splice() function inserts lst at location pos. If specified, the element(s) at
del or from start to end are removed.
Syntax:
The swap() function exchanges the elements in lst with those in the current list.
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.