Generic Programming and STL


Outline


Dictionary

STL
The C++ Standard Template Library
CGAL
The Computational Geometry Algorithm Library
Generic Programming
A programming paradigm with concepts (massivly uses templates)
Class Template
Specialization
Template Parameter
Concept
A set of requirements that a class must fulfill
Model
A concrete class that fulfills the requirements of a concept
Traits
Refinement
Generic Algorithms
Containers
Iterators
Generalization of pointers; objects that point to other objects.
Function Objects (Functors)
Adaptors
Allocators

Generic algorithms


A basic example: swap()

template <class T> void swap(T& a, T& b)
{
  T tmp = a; a = b; b = tmp;
}

A Simple Example: Version 1 - Standard C++

#include <stdlib.h>
#include <iostream>

inline int cmp(const void * a, const void * b)
{
  int aa = *(int *)a;
  int bb = *(int *)b;
  return (aa < bb) ? -1 : (aa > bb) ? 1 : 0;
}
 
int main(int argc, char * argv[])
{
  const int size = 1000;
  int array[size];
  int n = 0;
  while (std::cin >> array[n++]);
  n--;          // it got incremented once too many times
  qsort(array, n, sizeof(int), cmp);
  for (int i = 0; i < n; ++i)
    std::cout << array[i] << std::endl;
  return 0;
}
example1.cpp

STL Containers


A Simple Example: Version 2 - Containers, Iterators, Algorithms

#include <stdlib.h>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
 
int main(int argc, char * argv[])
{
  std::vector<int> v;           // create an empty vector of integers}
  int input;
  while (std::cin >> input)     // while not end of file}
    v.push_back(input);         // append to vector}
  std::sort(v.begin(), v.end());
  int n = v.size();
  for (int i = 0; i < n; ++i)
    std::cout << v[i] << std::endl;
  return 0;
}
example2.cpp

The vector container


Iterators

vector<int> v;
vector::iterator i1 = v.begin();
vector::iterator i2 = v.end();
while (i1 != i2) {
  ...
  ++i1; 
}

Iterator Types and Refinement


The sort() algorithm

#include <algorithm>

std::sort(first, beyond);
std::sort(first, beyond, std::greater<int>());

A Simple Example: Version 3 - iterator adaptors

#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

using namespace std;

int main(int argc, char * argv[])
{
  vector<int> v;
  istream_iterator<int> start(cin), end;
  back_insert_iterator<vector<int> > dest(v);
 
  copy(start, end, dest);
  sort(v.begin(), v.end());
  copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\n"));
  return 0;
}
example3.cpp

Adaptors


The istream_iterator adaptor


Typedefs

back_insert_iterator<vector<int> > dest(v);
versus
typedef vector<int> int_vector;
back_insert_iterator<int_vector> dest(v);

The copy() algorithm

Copy from standard input (from the current position in the input stream to the end of the stream) into a vector:
typedef istream_iterator<int> int_istream_iterator;
copy(int_istream_iterator(cin), int_istream_iterator(), v.begin());
This may cause an overflow though!

The back_insert_iterator adaptor


The ostream_iterator adaptor

top prev


Last modified: March 10 2005.