Skip to content

Instantly share code, notes, and snippets.

@tsukkee
Created January 25, 2011 13:11
Show Gist options
  • Select an option

  • Save tsukkee/794886 to your computer and use it in GitHub Desktop.

Select an option

Save tsukkee/794886 to your computer and use it in GitHub Desktop.
FizzBuzz with C++0x lambda
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
static const int SIZE = 100;
int main(int argc, char const* argv[])
{
using namespace std;
vector<string> v(SIZE);
int i = 1;
std::generate_n(v.begin(), SIZE, [&i] {
stringstream s;
if(i % 3 == 0) s << "Fizz";
if(i % 5 == 0) s << "Buzz";
if(i % 3 != 0 && i % 5 != 0) s << i;
++i;
return s.str();
});
std::copy(v.begin(), v.end(), ostream_iterator<string>(cout, "\n"));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment