您的位置:首页 > 其它

非类型模版参数

2015-12-28 11:02 197 查看
<span style="font-size:24px;"><span style="font-size:24px;">#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

template <typename T,int N>
void fn()
{
T array
= {0};
for(int i=0;i<N;i++)
{
array[i] = i+1;
cout << array[i] << " ";
}
cout << endl;
}
int main()
{
fn<int,10>();

fn<float,20>();   //可以动态调整数组大小   ,真棒

return 0;
}</span></span>


一个很牛逼的求和程序!!!转载的

<span style="font-size:24px;">#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

template <int N>
class Sum
{
private:

public:
static const int Value = Sum<N-1>::Value + N;
protected:
};

template <>
class Sum<1>
{
public:
static const int Value = 1;
};
int main()
{
cout << "求100内的和为 " << Sum<100>::Value <<endl;
return 0;
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: