previous | start | next

1.4 Using the template function simplesort:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include "simplesort.h"

using namespace std;

int main()
{
  int a[] = {5,2,3,10,7,1,4,8,9,6};
  int n = sizeof(a)/sizeof(int);

  simplesort(a, n); // compiler replaces T by int
  printArray(a, n);

  double b[] = {0.5,0.2,0.3,0.7,0.1,0.4,0.8,0.9,0.6};
  int nd = sizeof(b)/sizeof(double);

  simplesort(b, nd); // Compiler replaces T by double
  printArray(b, nd);

  string c[] = {"bb", "aa", "cb", "ca", "ba", "cd", "ab", "ac"};
  int ns = sizeof(c)/sizeof(string);

  simplesort(c, ns); // Compiler replaces T by string
  printArray(c, ns);

  return 0;
}

sizeof(a) is 40  (10 int's, each of size 4)
sizeof(int) is 4

sizeof(a)/sizeof(int) is 10  (the number of int's in a)


previous | start | next