Added versions of min() and max() that find the smallest and largest values in an iterable.

Sat, 23 Jul 2016 12:14:20 +0300

author
Teemu Piippo <teemu@compsta2.com>
date
Sat, 23 Jul 2016 12:14:20 +0300
changeset 161
1c483b54ddcb
parent 158
de7574d292ad
child 162
c9c0f1b62e42

Added versions of min() and max() that find the smallest and largest values in an iterable.

sources/basics.h file | annotate | diff | comparison | revisions
--- a/sources/basics.h	Fri Jul 22 17:52:23 2016 +0300
+++ b/sources/basics.h	Sat Jul 23 12:14:20 2016 +0300
@@ -78,12 +78,34 @@
 	return (a < b) ? a : b;
 }
 
+template<typename ContainerType>
+typename ContainerType::value_type min(const ContainerType& container)
+{
+	typename ContainerType::value_type result;
+
+	for (typename ContainerType::value_type element : container)
+		result = min(result, element);
+
+	return result;
+}
+
 template<typename T>
 T max (T a, T b)
 {
 	return (a > b) ? a : b;
 }
 
+template<typename ContainerType>
+typename ContainerType::value_type max(const ContainerType& container)
+{
+	typename ContainerType::value_type result;
+
+	for (typename ContainerType::value_type element : container)
+		result = max(result, element);
+
+	return result;
+}
+
 template<typename T>
 T clamp (T a, T b, T c)
 {

mercurial