Useful STL reference: http://www.sgi.com/tech/stl/
Partial list of containers in the STL:
Sequential Containers
Container Adaptors
|
Basic sequential operations: Object & back( ) void push_back( const Object & x ) void pop_back( ) Object & front( ) iterator insert( iterator pos, const Object & x ) iterator erase( iterator pos ) iterator erase( iterator start, iterator end ) deque and list also have: void push_front( const Object & x ) void pop_front( ) deque and vector also have: Object & operator[]( int index ) Object & at( int index ) int capacity( ) const
|
Associative Containers
|
All containers support:
|
= special objects that represent positions of elements within various STL containers (a generalization of pointers)
An iterator object is always associated with one specific type of container
iterator begin(); const_iterator begin() const; iterator end(); const_iterator end() const;
list<double> L;
L.push_back(1.2);
L.push_front(3.4);
L.insert(L.begin(), 5.6);
L.insert(L.end(), 7.8);
list<double>::const_iterator iter;
for (iter = L.begin(); iter != L.end(); ++iter)
cout << *iter << " ";
cout << endl;
input iterator : read only, single pass only
output iterator : write only, single pass only
forward (or basic) iterator : read and write access, multiple passes
bidirectional iterator : forward iterator plus can move in reverse
random-access iterator : bidirectional iterator plus can move > 1 element at a time
Input Iterator
Forward/Input/Output Iterator
Bidirectional Iterator
Random-acces Iterator
class IsPositive {
public:
bool operator() (int n) const {
return n > 0;
}
};
IsPositive test;
int x;
cout << "Enter an integer: ";
cin >> x;
if (test(x))
cout << x << " is positive" << endl;
else
cout << x << " is not positive" << endl;
Need to #include <algorithm>
Useful functions:
void sort( RandomAccessIterator begin, RandomAccessIterator end ); void stable_sort( RandomAccessIterator begin, RandomAccessIterator end );
Example:
// assume the print function has already been defined vector<int> V; V.push_back(4); V.push_back(8); V.insert(V.begin(), 12); V.insert(V.end(), 6); print(V); // prints: 12 4 8 6 sort(V.begin(), V.end()); print(V); // prints: 4 6 8 12
Useful functions:
InputIterator find( InputIterator begin, InputIterator end,
const EqualityComparable & x );
InputIterator find_if( InputIterator begin, InputIterator end,
Predicate pred );
Example:
vector<int>::iterator found, found1, found2, found3;
found = find(V.begin(), V.end(), 0);
if (found != V.end())
cout << *found << endl;
found1 = find_if(V.begin(), V.end(), IsPositive());
if (found1 == V.end())
cout << "no positive items" << endl;
else {
cout << *found1 << endl;
found2 = find_if(++found1, V.end(), IsPositive());
if (found2 == V.end())
cout << "no more positive items" << endl;
else {
cout << *found2 << endl;
found3 = find_if(++found2, V.end(), IsPositive());
if (found3 == V.end())
cout << " no more positive items" << endl;
else
cout << *found3 << endl;
}
}
Suppose V contains:
2 -8 0 -1 3 -5 -4 -9 1 7