美文网首页
★23.小窍门收集

★23.小窍门收集

作者: iDragonfly | 来源:发表于2017-06-30 22:26 被阅读0次

输出1-100

模板元编程

// 利用递归继承实现输出0-100
#include <iostream>

template <int N>
class X : public X<N - 1> {
public:
    X() { std::cout << N << std::endl; }
};

template <> class X<0> {};

int main() {
    X <100> x;
    return system("pause");
}

宏展开

// 利用宏展开实现输出0-100
#include <cstdio>
#include <cstdlib>

#define STEP1 ++n <= MAX ? printf("%d\n", n) : 0;
#define STEP2 STEP1 STEP1
#define STEP4 STEP2 STEP2
#define STEP8 STEP4 STEP4
#define STEP16 STEP8 STEP8
#define STEP32 STEP16 STEP16
#define STEP64 STEP32 STEP32
#define STEP128 STEP64 STEP64
#define STEP256 STEP128 STEP128

int n = 0;
const int MAX = 100;

int main() {
    STEP128;
    return system("pause");
}

相关文章

网友评论

      本文标题:★23.小窍门收集

      本文链接:https://www.haomeiwen.com/subject/mtaicxtx.html