One of the threads has a questions similar to my current challenge:
Write a program to find the maximum value of an arbitrary number of elements (integers if you must) without the use of the if-statement.
Now, one could use vargs and loop though using the max() function to find the max... but I wanted to challenge myself. Here was my first solution -- not the best but I like it for a first attempt.
#include <iostream>
#include <algorithm>
using namespace std;
/*
* The challange: find the maximum value of a arbitraty list of values wihtout
* the use of the conditional-control strucutes.
*/
template <class T>
class FindMax {
private:
T currentMax;
int state; //states 0 = no data; non-zero = current max stored.
public:
FindMax() : state(0) { }
void reset() {
state = 0;
}
FindMax& operator<<(const T& rhs) {
currentMax = std::max(state ? currentMax : rhs , rhs);
state = 1;
}
T& operator()() {
return currentMax;
}
};
int main() {
int a = 12, b= 37;
FindMax<int> themax;
themax << 1 << 2 << 10 << b << 6 << 36 << a;
cout << themax() << endl;
system("Pause");
return 0;
}
What I really would like to do is find a way to make it look more like a function and avoid the use of "<<".

New Topic/Question
Reply




MultiQuote






|