- #define NOMINMAX using std::min/max
- 4 Answers 4
- Not the answer you’re looking for? Browse other questions tagged c++ visual-studio winapi or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Possible problems with NOMINMAX on Visual C++
- 4 Answers 4
- Windows no min max
- Python3
- Efficient Rolling Max and Min Window
- 5 Answers 5
- Windows no min max
#define NOMINMAX using std::min/max
i recently added:
to my main.cpp in order to use
but i can’t use std::max()/std::min() in other files.
i tried to add #define NOMINMAX in my other files, but fails. what is the clue?
i looked around before asking, but i don’t understand the answer Possible problems with NOMINMAX on Visual C++
4 Answers 4
If you’re really desperate, put parentheses around the function names:
This syntax won’t apply a function-like macro. (Formally, to apply a function-like macro the name of the macro must be followed by optional white space then a ‘(‘.)
Define NOMINMAX via a compiler flag:
this will then be defined for all of the source files. I don’t use the IDEs but this page provides guidance on navigating the IDE to set this: Using STL in Windows Program Can Cause Min/Max Conflicts :
Simply define the NOMINMAX preprocessor symbol. This can be done in the Developer Studio project under Build, Settings, on the C/C++ tab, in the Preprocessor category. This will suppress the min and max definitions in Windef.h.
If you define NOMINMAX, because you prefer the STL version, then you may get problems while including gdiplus.h, which uses the min/max macro. As solution you need to include the STL headers and use «using namespace std» before you include the gdiplus.h.
It’s likely that your problem is that you #define NOMINMAX after you #include «windows.h» . It is important that the #define come first.
The reason is that windows.h (actually I think windef.h, which is included by windows.h) has code similar to this:
So #define NOMINMAX is telling the compiler (or actually the preprocessor) to skip over the definitions of min and max , but it will only apply if you do it before you #include «windows.h» .
Not the answer you’re looking for? Browse other questions tagged c++ visual-studio winapi or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Possible problems with NOMINMAX on Visual C++
What problems could I get when defining NOMINMAX before anything else in my program?
As far as I know, this will make not define the min and max macros such that many conflicts with the STL, e.g. std::min() , std::max() , or std::numeric_limits ::min() are resolved.
Am I right in the assumption that only Windows-specific and legacy code will have problems? Almost all libraries should not depend on min() and max() defined as macros?
Edit: Will there be be problems with other Windows headers?
4 Answers 4
Using NOMINMAX is the only not-completely-evil way to include . You should also define UNICODE and STRICT . Although the latter is defined by default by modern implementations.
You can however run into problems with Microsoft’s headers, e.g. for GdiPlus. I’m not aware of problems with headers from any other companies or persons.
If the header defines a namespace, as GdiPlus does, then one fix is to create a wrapper for the relevant header, where you include , and inside the header’s namespace, using namespace std; (or alternatively using std::min; and using std::max ):
Note that that is very different from a using namespace std; at global scope in header, which one should never do.
I don’t know of any good workaround for the case where there’s no namespace, but happily I haven’t run into that, so in practice that particular problem is probably moot.
Windows no min max
Given an integer array of size n, find the maximum of the minimum’s of every window size in the array. Note that window size varies from 1 to n.
Example:
Input: arr[] = <10, 20, 30, 50, 10, 70, 30>
Output: 70, 30, 20, 10, 10, 10, 10
The first element in the output indicates the maximum of minimums of all
windows of size 1.
Minimums of windows of size 1 are <10>, <20>, <30>, <50>, <10>,
<70>and <30>. Maximum of these minimums is 70
The second element in the output indicates the maximum of minimums of all
windows of size 2.
Minimums of windows of size 2 are <10>, <20>, <30>, <10>, <10>,
and <30>. Maximum of these minimums is 30
The third element in the output indicates the maximum of minimums of all
windows of size 3.
Minimums of windows of size 3 are <10>, <20>, <10>, <10>and <10>.
Maximum of these minimums is 20
Similarly, other elements of output are computed.
Naive Solution: Brute Force.
Approach: A simple brute force approach to solve this problem can be to generate all the windows possible of a particular length say ‘L’ and find the minimum element in all such windows. Then find the maximum of all such elements and store it. Now the length of window is 1
Python3
Output:
Complexity Analysis:
Efficient Solution: We can solve this problem in O(n) time. The idea is to use extra space. Below are detailed steps.
Step 1: Find indexes of next smaller and previous smaller for every element. Next smaller is the nearest smallest element on right side of arr[i]. Similarly, a previous smaller element is the nearest smallest element on the left side of arr[i].
If there is no smaller element on the right side, then the next smaller is n. If there is no smaller on the left side, then the previous smaller is -1.
For input <10, 20, 30, 50, 10, 70, 30>, array of indexes of next smaller is <7, 4, 4, 4, 7, 6, 7>.
For input <10, 20, 30, 50, 10, 70, 30>, array of indexes of previous smaller is <-1, 0, 1, 2, -1, 4, 4>
This step can be done in O(n) time using the approach discussed in next greater element.
Step 2: Once we have indexes of next and previous smaller, we know that arr[i] is a minimum of a window of length “right[i] – left[i] – 1”. Lengths of windows for which the elements are minimum are <7, 3, 2, 1, 7, 1, 2>. This array indicates, the first element is minimum in the window of size 7, the second element is minimum in the window of size 3, and so on.
Create an auxiliary array ans[n+1] to store the result. Values in ans[] can be filled by iterating through right[] and left[]
We get the ans[] array as <0, 70, 30, 20, 0, 0, 0, 10>. Note that ans[0] or answer for length 0 is useless.
Step 3: Some entries in ans[] are 0 and yet to be filled. For example, we know maximum of minimum for lengths 1, 2, 3 and 7 are 70, 30, 20 and 10 respectively, but we don’t know the same for lengths 4, 5 and 6.
Below are few important observations to fill remaining entries
a) Result for length i, i.e. ans[i] would always be greater or same as result for length i+1, i.e., ans[i+1].
b) If ans[i] is not filled it means there is no direct element which is minimum of length i and therefore either the element of length ans[i+1], or ans[i+2], and so on is same as ans[i]
So we fill rest of the entries using below loop.
Below is implementation of above algorithm.
Efficient Rolling Max and Min Window
I want to calculate a rolling maximum and minimum value efficiently. Meaning anything better than recalculating the maximum/minimum from all the values in use every time the window moves.
There was a post on here that asked the same thing and someone posted a solution involving some kind of stack approach that supposedly worked based on its rating. However I can’t find it again for the life of me.
Any help would be appreciated in finding a solution or the post. Thank you all!
5 Answers 5
The algorithm you want to use is called the ascending minima (C++ implementation).
To do this in C#, you will want to get a double ended queue class, and a good one exists on NuGet under the name Nito.Deque.
I have written a quick C# implementation using Nito.Deque, but I have only briefly checked it, and did it from my head so it may be wrong!
Here’s one way to do it more efficiently. You still have to calculate the value occasionally but, other than certain degenerate data (ever decreasing values), that’s minimised in this solution.
We’ll limit ourselves to the maximum to simplify things but it’s simple to extend to a minimum as well.
All you need is the following:
- The window itself, initially empty.
- The current maximum ( max ), initially any value.
- The count of the current maximum ( maxcount ), initially zero.
The idea is to use max and maxcount as a cache for holding the current maximum. Where the cache is valid, you only need to return the value in it, a very fast constant-time operation.
If the cache is invalid when you ask for the maximum, it populates the cache and then returns that value. This is slower than the method in the previous paragraph but subsequent requests for the maximum once the cache is valid again use that faster method.
Here’s what you do for maintaining the window and associated data:
Get the next value N .
If the window is full, remove the earliest entry M . If maxcount is greater than 0 and M is equal to max , decrement maxcount . Once maxcount reaches 0, the cache is invalid but we don’t need to worry about that until such time the user requests the maximum value (there’s no point repopulating the cache until then).
Add N to the rolling window.
If the window size is now 1 (that N is the only current entry), set max to N and maxcount to 1, then go back to step 1.
If maxcount is greater than 0 and N is greater than max , set max to N and maxcount to 1, then go back to step 1.
If maxcount is greater than 0 and N is equal to max , increment maxcount .
Go back to step 1.
Now, at any point while that window management is going on, you may request the maximum value. This is a separate operation, distinct from the window management itself. This can be done using the following rules in sequence.
If the window is empty, there is no maximum: raise an exception or return some sensible sentinel value.
If maxcount is greater than 0, then the cache is valid: simply return max .
Otherwise, the cache needs to be repopulated. Go through the entire list, setting up max and maxcount as per the code snippet below.
The fact that you mostly maintain a cache of the maximum value and only recalculate when needed makes this a much more efficient solution than simply recalculating blindly whenever an entry is added.
For some definite statistics, I created the following Python program. It uses a sliding window of size 25 and uses random numbers from 0 to 999 inclusive (you can play with these properties to see how they affect the outcome).
First some initialisation code. Note the stat variables, they’ll be used to count cache hits and misses:
Then the function to add a number to the window, as per my description above:
Next, the code which returns the maximum value from the window:
And, finally, the test harness:
Note that the test harness attempts to get the maximum for every time you add a number to the window. In practice, this may not be needed. In other words, this is the worst-case scenario for the random data generated.
Running that program a few times for pseudo-statistical purposes, we get (formatted and analysed for reporting purposes):
So you can see that, on average for random data, only about 3.95% of the cases resulted in a calculation hit (cache miss). The vast majority used the cached values. That should be substantially better than having to recalculate the maximum on every insertion into the window.
Some things that will affect that percentage will be:
- The window size. Larger sizes means that there’s more likelihood of a cache hit, improving the percentage. For example, doubling the window size pretty much halved the cache misses (to 1.95%).
- The range of possible values. Less choice here means that there’s more likely to be cache hits in the window. For example, reducing the range from 0..999 to 0..9 gave a big improvement in reducing cache misses (0.85%).
Windows no min max
hey. im writing on a project which includes and
for numeric_limits.
but when i want to do:
cin.ignore(numeric_limits::max());
it resolves ::max() to the max(a,b)-Macro defined in windows.h so i have to give it 2 parameters. but thats not waht i want.
yeah. I had that problem to. Just undef them:
There is also a define I think it is:
that prevents windows from dong that.
windows.h predates C++ in the Microsoft world by many years. It is the only file you need to include to get all the junk you need for Windows GUI programming.
The guys at Microsoft recognised this and added a set of switches you can define to turn off parts of it, the most generally useful being WIN32_LEAN_AND_MEAN. Anyway, defining NOMINMAX is the recomended way of switching off min/max macros.
thx. i´ll use NOMINMAX.
anyone knows what «Kanji» is. ther is an macro NOKANJI