FizzBuzz: Difference between revisions
Content added Content deleted
m (→{{header|C++}}) |
|||
Line 1,701: | Line 1,701: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
===minimal conditions=== |
===minimal conditions=== |
||
< |
<syntaxhighlight lang=cpp>#include <iostream> |
||
#include <chrono> |
#include <chrono> |
||
Line 1,740: | Line 1,740: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
===with modulo=== |
===with modulo=== |
||
< |
<syntaxhighlight lang=cpp>#include <iostream> |
||
using namespace std; |
using namespace std; |
||
Line 1,759: | Line 1,759: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===without modulo 15=== |
===without modulo 15=== |
||
< |
<syntaxhighlight lang=cpp>#include <iostream> |
||
using namespace std; |
using namespace std; |
||
Line 1,779: | Line 1,779: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===without modulo=== |
===without modulo=== |
||
Modulo can be expensive on some architectures. |
Modulo can be expensive on some architectures. |
||
< |
<syntaxhighlight lang=cpp>#include <iostream> |
||
int main() |
int main() |
||
Line 1,798: | Line 1,798: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
===using std::transform=== |
===using std::transform=== |
||
{{works with|C++11}} |
{{works with|C++11}} |
||
< |
<syntaxhighlight lang=cpp>#include <iostream> |
||
#include <algorithm> |
#include <algorithm> |
||
#include <vector> |
#include <vector> |
||
Line 1,825: | Line 1,825: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===metaprogramming=== |
===metaprogramming=== |
||
Version computing FizzBuzz at compile time with metaprogramming: |
Version computing FizzBuzz at compile time with metaprogramming: |
||
< |
<syntaxhighlight lang=cpp>#include <iostream> |
||
template <int n, int m3, int m5> |
template <int n, int m3, int m5> |
||
Line 1,875: | Line 1,875: | ||
fb_run<100> fb; |
fb_run<100> fb; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===hardcore templates=== |
===hardcore templates=== |
||
Compile with -ftemplate-depth-9000 -std=c++0x: |
Compile with -ftemplate-depth-9000 -std=c++0x: |
||
< |
<syntaxhighlight lang=cpp>#include <iostream> |
||
#include <string> |
#include <string> |
||
#include <cstdlib> |
#include <cstdlib> |
||
Line 2,041: | Line 2,041: | ||
std::cout << mpl::c_str<FizzBuzz<n>::type>::value << std::endl; |
std::cout << mpl::c_str<FizzBuzz<n>::type>::value << std::endl; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Note: it takes up lots of memory and takes several seconds to compile. To enable compilation for 7 < n <= 25, please, modify include/boost/mpl/limits/string.hpp BOOST_MPL_LIMIT_STRING_SIZE to 128 instead of 32). |
Note: it takes up lots of memory and takes several seconds to compile. To enable compilation for 7 < n <= 25, please, modify include/boost/mpl/limits/string.hpp BOOST_MPL_LIMIT_STRING_SIZE to 128 instead of 32). |
||