Created
January 25, 2011 13:11
-
-
Save tsukkee/794886 to your computer and use it in GitHub Desktop.
FizzBuzz with C++0x lambda
This file contains hidden or 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
| #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