• Advertisement
Sign in to follow this  
  • entries
    72
  • comments
    38
  • views
    22279

hi

Sign in to follow this  

348 views

Templates give me this feeling, like you can do anything with them. But I feel like I dont know what to do with them. So I have been just writing generic algorithms, the first few I picked up off of DADS 'M' section, and the rest have been just off the top of my head ones. Nothing really special, and I think I have already overwritten/deleted a few, but I still wanted to share them for anyone who needs some examples. So here is my little bit of template functions.

using namespace std;

template <class T>
class Stupid
{
public:
T data;
int cnt;
Stupid()
{
data = T();
cnt = 0;
}
};

template <class T>
class Trouble
{
T data;
public:
Trouble(T d) { data=d; }
T get() { return data; }
};

template <>
class Trouble <int>
{
int data;
public:
Trouble(int d) { data=d; }
Trouble() { data = 1; }
int get();
};

int Trouble<int>::get()
{
return data + 12;
}

template
T Mean( FwItr b, FwItr e )
{
T sum = T();
T cnt = T();

for(; b != e; ++b, ++cnt) sum += *b;
return cnt ? sum/cnt : T();
}

template
T Midrange( FwItr b, FwItr e )
{
typedef typename std::iterator_traits::value_type value_type;
value_type min(*b), max(*b);

for(; b != e; ++b)
{
if( *b > max )
max = *b;
if( *b < min )
min = *b;
}
return T( min + max ) / 2;
}

template
vector Mode( FwItr b, FwItr e )
{
vector modeList;
bool moding = true;

vector > StupidList ( e - b );
vector >::iterator itera;

cout << (T)StupidList.size() << endl;

for(;b != e;++b)
{
//find(StupidList.begin(), StupidList.begin(), *b);

}
return modeList;
}

template <class T>
vector RandVect( T min, T max, int num )
{
vector store (num);
vector::iterator b = store.begin();
vector::iterator e = store.end();
srand((unsigned)time(NULL));
for(;b!=e;++b)
*b = rand() % (max - min) + min;
return store;
}
template <class T, typename FwItr>
T Arcane(FwItr b, FwItr e)
{
T t = T();
for(;b != e; ++b)
{
t += (T)(0.5 * *b);
}
return t;
}



Not much there really, and nothing special (Mode() does not actually preform said function). RandVect is the most recent, and I guess its the one I actually use. I wanted to pump vectors of random numbers through the math functions, so I decided that would be a great use of templates.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement