The header file # include <list> must be included in the list,
1) how to define a list object
# Include <list>
Int main (void)
{
List <char> clist; // declares an instance of the List <char> template class.
}
2) use the list member functions push_back and push_front to insert an element to the list.
Clist. push_back ('A'); // put an object behind a list
Clist. push_front ('B'); // put an object in front of a list
3) use the list member function empty () to determine whether the list is empty.
If (clist. Empty () {printf ("This list is empty ");}
4) Use List <char >:: iterator to obtain the pointer to list.
List <char >:: iterator chariterator;
For (citerator = clist. Begin (); citerator! = Clist. End (); citerator ++)
{
Printf ("% C", * citerator );
} // Output all objects in the list
Note: clist. begin () and clist. the end () function returns a pointer to list <char>: iterator. Because list adopts a linked list structure, it does not support random access, so clist cannot be used. begin () + 3 points to the fourth object in the list. Vector and deque support random access.
5) use the general STL algorithm count () to count the number of elements in the list.
Int cnum;
Char CH = 'B ';
Cnum = count (clist. Begin (), clist. End (), ch); // count the number of characters B in the list
Note: # include <algorithm> must be added before using the count () function.
6) use the general STL algorithm count_if () to count the number of elements in the list.
Const char C ('C ');
Class ISC
{
Public:
Bool operator () (char & Ch)
{
Return CH = C;
}
};
Int numc;
Numc = count_if (clist. Begin (), clist. End (), ISC (); // count the number of C;
Note: count_if () includes a function object parameter. The function object is a class function object with at least one operator () method. It is agreed that true or false will be returned when the STL algorithm calls operator. They determine this function based on this. For example, we will make it clearer. Count_if () evaluates more complex than count () by passing a function object to determine whether an object should be counted.
7) use the general STL algorithm find () to search for objects in the list.
List <char >:: iterator finditerator;
Finditerator = find (clist. Begin (), clist. End (), 'C ');
If (finditerator = clist. End ())
{
Printf ("not find the char 'C '!");
}
Else
{
Printf ("% C", * finditerator );
}
Note: If the specified object is not found, the value of clist. End () is returned. If it is found, a pointer to the iterator object is returned.
8) use the general STL algorithm find_if () to search for objects in the list.
Const char C ('C ');
Class C
{
Public:
Bool operator () (char & Ch)
{
Return CH = C;
}
};
List <char>: iterator finditeratorfinditerator = find_if (clist. Begin (), clist. End (), ISC (); // query string C;
Note: If the specified object is not found, the value of clist. End () is returned. If it is found, a pointer to the iterator object is returned.
9) use the list member function sort () for sorting
Clist. Sort ();
10) insert an object to the list using the list member function.
Clist. insert (cliset. End, 'C'); // Insert the character 'C' at the end of the list'
Char ch [3] = {'A', 'B', 'C '};
Clist. insert (clist. End, & Ch [0], & Ch [3]); // insert three characters to the list
Note: The insert () function inserts one or more elements into the specified iterator position. The element appears before the position specified by iterator.
11) how to delete elements in the list
Clist. pop_front (); // Delete the first element
Clist. pop_back (); // Delete the last element
Clist. Erase (clist. Begin (); // use iterator to delete the first element;
Clist. Erase (clist. Begin (), clist. End (); // use iterator to delete all elements;
Clist. Remove ('C'); // use the remove function to delete the specified object;
List <char>: iterator newend; // Delete All 'C' and return
Iteratornewend = clist. Remove (clist. Begin (), clist. End (), 'C ');