C++ Standard Library
The STL contains:
·
class
templates for containers, (e.g stack, queue )
·
generic
algorithms using the containers (e.g. sort, reverse )
·
iterators for traversing a container
Some of the
class templates in the STL are:
stack
queue
list linked list
map hash table
vector dynamic sized array
set
E.g.
template class<T> class stack
{
public:
stack();
stack( const stack& );
void push( const T& );
void pop();
T& top();
bool empty() const;
unsigned int size() const;
stack&
operator=( const stack& );
bool operator==( const
stack&, const stack& );
...
private:
...
};
The generic
algorithms are
function templates that can be used with STL class templates.
For
example: find( begin,
end, item )
template< class Titerator, class Titem >
Titerator find( Titerator
begin, Titerator end, const Titem&
item );
For an int[] array, Titem is int, and Titerator is int*.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[] = { 12, 14, 10 };
int* pos = find( a, a+3, 14 );
cout << *pos;
}
More
generally, find() uses an iterator
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main()
{
list<int> myList;
myList.push_back(
12 ); // enqueue
myList.push_back(
14 );
myList.push_back(
10 );
for (list<int>::iterator iter = myList.begin();
iter != myList.end();
iter++)
{
cout << *iter
<< ' ';
}
}