Generic Code
Example (from the C++ Standard Template Library)
template < class T > class vector { // vector implementation ... };
vector<int> a(7, 3); //
creates vector of 7 ints, initialized to 3
vector<float> b(7, 1.0); // creates vector of 7 floats, initialized
to 1.0
vector<int> a(10); // creates
vector of 10 ints, default initialized (to 0)
vector<float> b(10); // creates vector of 10
floats, default initialized (to 0.0)
class complex
{
double real_part;
double imaginary_part;
public:
complex();
// default constructor
complex(double real, double
imaginary); // sets real and imaginary parts
complex(const complex
&);
// copy constructor
complex &operator=(const complex
&); // assignment operator
// other methods ...
};
vector<complex> a(10, complex(1.0, 2.0));
// creates a vector of 10 complex values, initialized to 1 + 2i
vector <complex>
b;
b =
a;
// vector assignment uses complex :: operator=
Consider alternative with run-time polymorphism:
class Object // abstract base instead of generic template type
{
public:
virtual Object *clone() =
0;
// equivalent to copy constructor
virtual void assign(const Object &) = 0; //
like assignment operator
// other methods
};
class Vector // stores pointers to Objects
{
// will call init_val.clone() to create copies
Vector(int num_elements, const Object &init_val);
// will invoke clone() on elements of rhs to create copies of elements
Vector(const Vector &rhs);
// will invoke assign() on elements of (*this) to copy element values from rhs
Vector &operator=(const Vector &rhs);
};
class Integer: public Object // sample Object implementation
{
int value;
public:
Integer(): value(0) {}
Integer(const Integer &rhs): value(rhs.value)
{}
Integer *clone() { return new Integer(*this); }
// implements Object::clone()
void assign(const Object
&o)
// implements Object::assign()
{
Integer *i =
dynamic_cast<Integer *>(&o); // does run time check
if (i !=
0)
// null returned if not an Integer
assign(*i);
// calls Integer-specific assign
else
// throw some exception
}
void assign(const Integer &i) { value = i.value; }
// Integer-specific assign
};
Vector a(10, Integer(3)); // creates a vector of 10 Integers with value 3
Compare to generic vector<> template:
C++ Standard Template Library
STL Containers
#include <vector> // vector class header
#include <list> // list class header
using namespace std;
vector < int > v(10); // fills v with 10
default ints (0)
list < float > l(10); // fills l
with 10 default floats (0.0)
vector < int > :: iterator v_iter = v.begin(); // points to first
element of v
list < float > :: iterator l_iter = l.end();
// points to one-past-end of l
Example:
template < class T, class iterator_type >
void fill_range ( iterator_type begin, iterator_type end, T
value)
{
while (begin
!= end)
*begin++ = value;
}
fill_range(v.begin(), v.end(),
7) // fills vector v with value
7
fill_range(l.begin(), l.end(), 6.8) //
fills list l with value 6.8
complex a[20];
fill_range(a, a + 20, complex(3, 7)) // fills array of complex with value 3 + 7i
Run-time polymorphism version of iterators would look like:
class Iterator
{
virtual void increment() =
0;
// like operator++
virtual void decrement() =
0;
// like operator--
virtual Object ¤t_element() =
0; // like operator*
};
STL Algorithms
#include <algorithm>
using namespace std;
complex b[15];
copy(a, a + 15, b); // specify beginning and end of source, beginning of
destination
// copies first 15 elements of a to b
sort(v.begin(), v.end()) // sorts elements of vector v
// requires that element type implements operator <