Examples in Template Programming

overload_template.cpp demonstrates how to use typeid, and how to use template specialization

generate.cpp demonstrates the STL algorithm, generate

sort.cpp demonstrates the STL algorithm sort, and also demonstrates how to use it with a functor, or predicate

transform.cpp demonstrates the STL algorithm, transform

functor.cpp demonstrates how to use functors in C++. This code is a fixed of version of Nabeel Mohamed's code.

mypair.cpp demonstrates the use of the keyword typename, of the boost command, add_reference.

pair.cpp demonstrates that std::pair won't compile

max_typename.cpp demonstrates the use of the typename to define a template function

TimeCopy.cpp times std::copy versus a hand coded copy function. This program also demonstrates the use of the boost::timer class, defined in boost/timer.hpp. Note that the arguments in std::copy are in reverse order compared to memcpy, memmove:

Prototype Include file
FooClass * std::copy( const FooClass *from, const FooClass *from_end, FooClass *to); algorithm or algo.h
void *memcpy(void *to, const void *from, size_t n); string.h
void *memmove(void *to, const void *from, size_t n); string.h