Last active
November 27, 2020 15:53
-
-
Save Trass3r/d7c41f7b5bc113fd5b174b57c0a0b246 to your computer and use it in GitHub Desktop.
comparing implementations of 'is x in a set of numbers' - https://godbolt.org/z/9en4qv
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
int baseline(int x) | |
{ | |
switch (x) | |
{ | |
case 0: | |
case 2: | |
case 8: | |
case 15: | |
case 16: | |
return 5; | |
} | |
return x * x; | |
} | |
#include <array> | |
#include <algorithm> | |
int usinglist(int x) | |
{ | |
const std::array<int, 5> s = { | |
0, 2, 8, 15, 16 | |
}; | |
if (std::any_of(s.begin(), s.end(), | |
[x](int n){ return n == x; })) | |
return 5; | |
return x * x; | |
} | |
#include <set> | |
int usingset(int x) | |
{ | |
const std::set<int> s = { | |
0, 2, 8, 15, 16 | |
}; | |
if (s.count(x)) | |
return 5; | |
return x * x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment