Created
June 14, 2012 15:01
-
-
Save kazh98/2930865 to your computer and use it in GitHub Desktop.
C++ Lambda Test
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 <cstdio> | |
| #include <cstring> | |
| #include <functional> | |
| #define N 20 | |
| using namespace std; | |
| static | |
| int k_operafan[ N + 1 ]; | |
| int | |
| main ( | |
| int argc, | |
| char * argv[ ] | |
| ) | |
| { | |
| function<int ( int )> fib = [&fib] ( | |
| const int n | |
| ) | |
| { | |
| if ( n <= 2 ) return ( 1 ); | |
| else if ( ~k_operafan[ n ] ) ; | |
| else | |
| { | |
| k_operafan[ n ] = fib ( n - 2 ) + fib ( n - 1 ); | |
| } | |
| return ( k_operafan[ n ] ); | |
| }; | |
| memset ( k_operafan, -1, sizeof ( k_operafan ) ); | |
| printf ( "%dth Fibonacci Number = %d\n", N | |
| , fib ( N ) ); | |
| return ( 0 ); | |
| } | |
| /** Result: | |
| 20th Fibonacci Number = 6765 | |
| */ |
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 <cstdio> | |
| #include <iterator> | |
| #include <functional> | |
| #define N 20 | |
| using namespace std; | |
| template<class RandomAccessIterator> | |
| void | |
| sort ( | |
| RandomAccessIterator begin, | |
| RandomAccessIterator end | |
| ) | |
| { | |
| typedef RandomAccessIterator I; | |
| typedef typename iterator_traits<I>::value_type R; | |
| function<void(I,I)> swap = [ ] ( | |
| RandomAccessIterator a, | |
| RandomAccessIterator b | |
| ) | |
| { | |
| R t = *a; | |
| *a = *b; | |
| *b = t; | |
| return ; | |
| }; | |
| function<void(I,I,function<void(void)>)> recur = [ &recur, &swap ] ( | |
| RandomAccessIterator b, | |
| RandomAccessIterator e, | |
| function<void(void)> co | |
| ) | |
| { | |
| if ( b >= e ) | |
| { | |
| return | |
| co ( ); | |
| } | |
| else | |
| { | |
| const R p = *b; | |
| I h = b, t = e; | |
| while ( h <= t ) | |
| { | |
| while ( *h < p ) ++h; | |
| while ( *t > p ) --t; | |
| if ( h > t ) break ; | |
| swap ( h, t ); | |
| ++h; --t; | |
| } | |
| return | |
| recur ( b, t, [ &recur, &h, &e, &co ] ( | |
| void | |
| ) | |
| { | |
| recur ( h, e, co ); | |
| } ); | |
| } | |
| }; | |
| return | |
| recur ( begin, end, [ ] ( void ) { return ; } ); | |
| } | |
| int | |
| main ( | |
| int argc, | |
| char * argv[ ] | |
| ) | |
| { | |
| int a[ N ] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 | |
| , 5, 8, 9, 7, 9, 3, 2, 3, 8, 4 }; | |
| int i; | |
| for ( i = 0; i < N; ++i ) | |
| { | |
| if ( i ) putchar ( ' ' ); | |
| printf ( "%d", a[ i ] ); | |
| } | |
| putchar ( '\n' ); | |
| sort ( a, a + N ); | |
| for ( i = 0; i < N; ++i ) | |
| { | |
| if ( i ) putchar ( ' ' ); | |
| printf ( "%d", a[ i ] ); | |
| } | |
| putchar ( '\n' ); | |
| return ( 0 ); | |
| } | |
| /** Result: | |
| 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 | |
| 1 1 2 2 3 3 3 3 4 4 5 5 5 6 7 8 8 9 9 9 | |
| */ |
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 <cstdio> | |
| using namespace std; | |
| int | |
| main ( | |
| int argc, | |
| char * argv[ ] | |
| ) | |
| { | |
| printf ( "I DON'T LOSE TO ERIC!!!=> %d\n" | |
| , ( [] ( const int R ) { return ( R * R ); } ) ( 3 ) ); | |
| return ( 0 ); | |
| } | |
| /** Result: | |
| I DON'T LOSE TO ERIC!!!=> 9 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment