Skip to content

Instantly share code, notes, and snippets.

@rhysd
Created July 3, 2012 07:31
Show Gist options
  • Save rhysd/3038277 to your computer and use it in GitHub Desktop.
Save rhysd/3038277 to your computer and use it in GitHub Desktop.
FizzBuzz
#include <iostream>
#include <array>
#include <string>
#include <algorithm>
#include <numeric>
int main()
{
std::array<size_t, 100> arr;
std::iota(arr.begin(), arr.end(), 1);
std::array<std::string, 100> fizzbuzz;
std::transform(arr.begin(), arr.end(), fizzbuzz.begin(),
[](size_t const i) -> std::string
{
return i%15==0 ? "FizzBuzz" :
i%3==0 ? "Fizz" :
i%5==0 ? "Buzz" :
/*else*/ std::to_string(i);
}
);
std::for_each(fizzbuzz.begin(), fizzbuzz.end(),
[](std::string const& str)
{
std::cout << str << std::endl;
}
);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment