当我从命令行得到一个参数时,我应该使用该参数来确定数组,矩阵和向量的类型。我们的想法是将函数初始化为void *,然后将类型转换为特定的基本类型。
它应该做这样的事情:
void initMatrix1D(void *mat, int c, int m, int arg){
if(arg==200){
(unsigned char*)mat;
for(int j=0; j<c; j++){
mat[j]=rand()%m;
}
}
else if(arg==1000){
for(int j=0; j<c; j++){
(unsigned short*)mat[j]=rand()%m;
}
}
else{
cout<<"No existing type for "<<arg<<". Program will now close."<<endl;
exit(1);
}
}
当然,这不起作用。有人可以给我一个如何做的暗示吗?