C++,如何模板函数里面使用 常规参数?

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 15:14   17   0
C++,如何模板函数里面使用 常规参数?
C++,如何模板函数里面使用 常规参数?举个简单的例子来看看,如何声明定义和调用带有常规参数的函数吧。
匿名 | 浏览 76 次 2015-08-17 21:39
2015-10-07 13:28 最佳答案
#include <iostream>
using namespace std;

template<typename T,int R,int C>
void ada(T (*data)[C])
{
for(int i=0;i<R;i++)
{
data=data+i; 
for(int j=1;j <C;j++)
{
(*data)[0]+=(*data)[j];
}
}

}
void main()
{
int a[2][5]={{1,3,5,7,9},{2,4,6,8,10}};
ada<int,2,5> (a);         //常规参数
for(int i=0;i<2;i++)
{
for(int j=0;j<5;j++)
cout<<a[i][j]<<"  ";
cout<<endl;
}

}

example 2:

#include  <iostream>
using namespace std; 

template<typename t1,int n>
t1 s(t1 a[]){
 t1 sum=0;
 for(int i=0;i<n;i++){
  sum+=a[i]*a[i];
 }

 return sum;
}

void main(){
 int x[]={1,2,3,4,5};
 cout<<s<int,5>(x)<<endl;  //常规参数
}
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP