Dave's Brain

Browse - Programming Tips - How can I do min/max in Windows?

Date: 2013jan2
OS: Windows
Language: C/C++

Q.  How can I do min/max in Windows?

A.  The windows.h defines min() and max() macros which are ok in most situations.
#include "windows.h" int smaller = min(a, b); int bigger = min(a, b);
But they aren't perfect since they are macros they might eval your parameters. Don't use so anything like min(a++, --b) So its better to use the STL functions:
#include <algorithm> int smaller = (std::min<int>)(a, b); int bigger = (std::min<int>)(a, b);
If you are using the STL function you might want to do this:
#define NOMINMAX #include <windows.h>
Which stops the min/max macros from being defined. Finally you can define it yourself:
template<class T> T min(T a, T b) { return a < b ? a : b; } template<class T> T max(T a, T b) { return a > b ? a : b; }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.