Created
April 19, 2011 19:50
-
-
Save CrBoy/929439 to your computer and use it in GitHub Desktop.
print out a palindromic stack implemented by template meta programming XD
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 <string> | |
#include <iomanip> | |
using namespace std; | |
template<size_t max> string inc_seq() | |
{ | |
string s; | |
for(int i=1; i<=max; i++) | |
s += '0'+i; | |
return s; | |
} | |
template<> string inc_seq<1>() { return string("1"); } | |
template<size_t max> string dec_seq() | |
{ | |
string s; | |
for(int i=max; i>=1; i--) | |
s += '0'+i; | |
return s; | |
} | |
template<> string dec_seq<1>() { return string("1"); } | |
template<size_t level> void PalindromicStack(size_t center = level) | |
{ | |
PalindromicStack<level-1>(center); | |
cout << setw(center) << inc_seq<level>() << dec_seq<level-1>() << endl; | |
//cout << setw(center) << Seq<level>::inc << Seq<level-1>::dec << endl; | |
} | |
template<> void PalindromicStack<1>(size_t center) | |
{ | |
cout << setw(center) << 1 << endl; | |
} | |
int main(int argc, const char *argv[]) | |
{ | |
PalindromicStack<5>(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment