Last active
July 19, 2019 11:41
-
-
Save B1Z0N/8c5780b1158c0bc67c45bbf4c55cff16 to your computer and use it in GitHub Desktop.
finding maximal and minimal element in variadic number of arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// finding maximal and minimalelement in variadic number of arguments | |
#include <iostream> | |
namespace cmns { | |
// commons | |
// *************MAXIMAL*VALUE*FUNCTIONS**************** | |
/** | |
* Recursion stop on oneelemnt sequence | |
*/ | |
template <typename T> | |
T max( const T& elem ) { return elem; } | |
/** | |
* Recursive function to find maximal value in | |
* arguments passed | |
*/ | |
template <typename T, typename... Args> | |
auto max( T fst, Args... args ) | |
{ | |
auto max_elem { max( args... ) }; | |
return max_elem > fst ? max_elem : fst; | |
} | |
// *************MINIMAL*VALUE*FUNCTIONS**************** | |
/** | |
* Recursion stop on oneelemnt sequence | |
*/ | |
template <typename T> | |
T min( const T& elem ) { return elem; } | |
/** | |
* Recursive function to find minimal value in | |
* arguments passed | |
*/ | |
template <typename T, typename... Args> | |
auto min( T fst, Args... args ) | |
{ | |
auto max_elem { min( args... ) }; | |
return max_elem < fst ? max_elem : fst; | |
} | |
}; | |
#include "maxminvar.cpp" | |
int main() | |
{ | |
bool res; | |
res = ( cmns::max( 3, -3, 2 ) == 3 ); | |
if( !res ) goto err; | |
res = ( cmns::min( 3, -3, 2 ) == -3 ); | |
if( !res ) goto err; | |
res = ( cmns::max( -45, -3 ) == -3 ); | |
if( !res ) goto err; | |
res = ( cmns::min( -45, -3 ) == -45 ); | |
if( !res ) goto err; | |
res = ( cmns::max( -3 ) == -3 ); | |
if( !res ) goto err; | |
res = ( cmns::min( -3 ) == -3 ); | |
if( !res ) goto err; | |
std::cout << "MaxMinVar test: success!\n"; | |
return 0; | |
err: | |
std::cout << "MaxMinVar test: fail!\n"; | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment