本内容接上题,上题的写法是写出一个求出logk(int n)的整数部分的template,结果如下:

#include <iostream>
using namespace std;
template <int n>
class static_log2N
{
    public:
        enum {value = static_log2N<n/2>::value+1 };
}    ;

template <>
class static_log2N<1>
{
    public:
        enum{value = 0};
};

int main()
{
    cout<<static_log2N<28>::value<<endl;
    cin.get();
}       

进一步扩展,可以写出一个求出底为k,求log(int k)(int N)的整数部分的程序来,如下:

#include <iostream>
using namespace std;
template <int base,int n>
class static_logxN
{
    public:
        enum {value = static_logxN<base,n/base>::value+1 };
}    ;

template <int base>
class static_logxN<base,1>
{
    public:
        enum{value = 0};
};

int main()
{
    cout<<static_logxN<3,28>::value<<endl;
    cin.get();
}       

   昨天去书店找关于此类编程的资料,在c++ templates一书中找到称其为metaprogamming, 呵呵,孤陋寡闻了, 这本书的中文版太贵了,而且网上评论说译的不是太好,于是上午就买了本影印版,看上去是有点费劲,不过可省了近20块大洋呢^_^  趁这几天有时间,抓紧看一下:)

Fixed issues:

  • 当行首出现类似*p 的语句时回车仍会补*, 本次更新检查如果行首的*后面紧跟的为非空格的话, 回车不自动补*
  • 在*或//后自动填充适当空格以和上行保持一致, 大致如下所示:


// This line are many leading spaces after "//"
// | (cursor will automatically move to the same column as "T"


© 2007 pangwa's Blog | iKon Wordpress Theme by Windows Vista Administration | Powered by Wordpress