STL学习-Trait

// Sample2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "time.h"
#include
#include
#include
#include
#include
using namespace std;

#define  COUNT 100000

void set_value(int& i) { i = rand(); }

template
class trait_sort
{
public:
 static void sort_data(V& v)
 {
  sort(v.begin(), v.end());
 }
} ;

template <>
class trait_sort < list >
{
public:
 static void sort_data(V& v)
 {
  v.sort();
 }
} ;

template
class trait_print
{
public:
 static void print_result(unsigned long t)
 {
  cout << "cost time for vector sorting: " << t << "ms" << endl;
 }
} ;


template <>
class trait_print < list >
{
public:
 static void print_result(unsigned long t)
 {
  cout << "cost time for list sorting: " << t << "ms" << endl;
 }
} ;

template <>
class trait_print < deque >
{
public:
 static void print_result(unsigned long t)
 {
  cout << "cost time for deque sorting: " << t << "ms" << endl;
 }
} ;

template , class P = trait_print >
struct sorttimer
{
 operator() (V& v)
 {
  v.swap(V(COUNT));
  for_each(v.begin(), v.end(), set_value);
  
  unsigned long t1 = GetTickCount();  
  trait_sort::sort_data(v);
  unsigned long t2 = GetTickCount();
  
  trait_print::print_result(t2 - t1);
 }
};


int main(int argc, char* argv[])
{
 srand(unsigned(time(NULL)));
 
 sorttimer< vector >()(vector());
 sorttimer< list >()(list());
 sorttimer< deque >()(deque());
 
 return 0;
}
 

你可能感兴趣的:(软件开发)