Slice2Matrix

本文档将以切片数据为例介绍读入文本格式数据,并将一维属性值写为二维阵列的过程。

实际工区中的切片常常是不规则的,因此在将其转换为二维阵列的过程中,需将切片填充为一个规则的矩形。

需要用到的文件:

1-4 SliceData IO.md中建立的头文件SliceDataIO.h与C++文件SliceDataIO.cpp

新建头文件SliceDataIO.h与C++文件SliceDataIO.cpp,以及主函数main.cpp

1 编写头文件SliceDataMatrix.h

1.1 程序描述、调用、声明、定义

/**********************************************************************

 * Copyright(C) 2018,Company All Rights Reserved
*
* @file : SliceDataMatrix.cpp
*
* @brief : 实现切片数据向二维阵列的转换
*
* @version : 1.0
*
* @author : Fan XinRan
*
* @date : 2022/2/8 星期二
*
* Others :
**********************************************************************/ #pragma once
#include"SliceDataIO.h" //SliceDataIO.h中包含了需要的标准库,全局常量等

1.2 声明最大、最小值结构体

此结构体用于存放最大/最小 Line/CDP的值及索引。

typedef struct MaxminResultInt{
int value;
int index;
} MaxminResultInt;

1.3 声明SliceDataMatrix类

class SliceDataMatrix{

public:
SliceDataMatrix(); //构造函数
~SliceDataMatrix(); //析构函数 bool setData(SliceDataIO *datainput,int incLine,int incCDP); // set()方法
bool writeData(float **dataoutput,int nx,int ny,const char *filenameoutput); // 写入方法
float **getTimeMatrix(); //在实际应用时,通常需要的是Time和Value这两个属性
float **getValueMatrix();
bool **getFlagMatrix();
int getNumLine();
int getNumCdp();
private: int _nline; // 声明:总线数
int _ncdp; // 总道数
int _IncLine; // 线采样间隔
int _IncCDP; // 道采样间隔 int _maxLineIndex; // 声明: 最大线号
int _minLineIndex; // 最小线号
int _maxCDPIndex; // 最大道号
int _minCDPIndex; // 最小道号 int **_sliceIndexdataMatrix; // 声明: 1 切片号二维阵列
int **_lineIndexMatrix; // 2 线号...
int **_cdpIndexMatrix; // 3 道号...
float **_Xmatrix; // 4 X坐标...
float **_Ymatrix; // 5 Y坐标...
float **_TimeMatrix; // 6 时间...
float **_ValueMatrix; // 7 属性值... bool **_flagMatrix; // 标识矩阵,用于标记该点是否有值 MaxminResultInt _maxminInt(int *dataInput, int n, bool flag); // 新建一个存放最大最小值的结构体对象 };

2 编写C++文件SliceDataMatrix.cpp

2.1 定义构造函数与析构函数

SliceDataMatrix::SliceDataMatrix(){

	this->_IncCDP = 0;	        // 定义取样间隔和总线、道数
this->_IncLine = 0;
this->_ncdp = 0;
this->_nline = 0; this->_minCDPIndex = 0; // 定义最大||最小 线号||道号
this->_maxCDPIndex = 0;
this->_minLineIndex = 0;
this->_maxLineIndex = 0; this->_sliceIndexdataMatrix = NULL; // 定义slice相关的Matrix
this->_cdpIndexMatrix = NULL;
this->_lineIndexMatrix = NULL;
this->_TimeMatrix = NULL;
this->_ValueMatrix = NULL;
this->_Xmatrix = NULL;
this->_Ymatrix = NULL; this->_flagMatrix = NULL; // 定义标识矩阵
}
// 析构函数
SliceDataMatrix::~SliceDataMatrix(){ if(this->_sliceIndexdataMatrix!=NULL){
free2int(_sliceIndexdataMatrix);
this->_sliceIndexdataMatrix = NULL;
} if(this->_cdpIndexMatrix!=NULL){
free2int(_cdpIndexMatrix);
this->_cdpIndexMatrix = NULL;
} if (this->_lineIndexMatrix != NULL) {
free2int(_lineIndexMatrix);
this->_lineIndexMatrix = NULL;
} if (this->_Xmatrix != NULL) {
free2float(_Xmatrix);
this->_Xmatrix = NULL;
} if (this->_Ymatrix != NULL) {
free2float(_Ymatrix);
this->_Ymatrix = NULL;
} if (this->_TimeMatrix != NULL) {
free2float(_TimeMatrix);
this->_TimeMatrix = NULL;
} if (this->_ValueMatrix != NULL) {
free2float(_ValueMatrix);
this->_ValueMatrix = NULL;
} if (this->_flagMatrix != NULL) {
free2bool(_flagMatrix);
this->_flagMatrix = NULL;
}
}

2.2 定义方法函数

(1) set()方法

bool SliceDataMatrix::setData(SliceDataIO *datainput, int incLine, int incCDP){

    // 此时输入为SliceDataIO类对象

	if(datainput==NULL){			// 判空
return false;
} if((incLine<0)||(incCDP<0)){ // 判定传入的采样间隔是否合法
return false;
}
// 通过SliceDataIO类中的get()方法获取基础数据...
int *slice = datainput->getSliceIndex(); // 切片号
int *lineIndex = datainput->getLineIndex(); // 线号
int *cdpIndex = datainput->getCDPIndex(); // 道号
float *X = datainput->getX(); // X坐标
float *Y = datainput->getY(); // Y坐标
float *time = datainput->getTime(); // time值
float *value = datainput->getValue(); // value值 int nsample = datainput->getSampleNum(); // nsample采样点 int index_Xtemp = 0; // 线、道号在阵列中的重新定位
int index_Ytemp = 0; MaxminResultInt maxline; // 针对切片4个角点,新建四个结构体
MaxminResultInt maxcdp;
MaxminResultInt minline;
MaxminResultInt mincdp; maxline = this->_maxminInt(lineIndex, nsample, true); // 计算出最大/最小的值、索引
maxcdp = this->_maxminInt(cdpIndex, nsample, true);
minline= this->_maxminInt(lineIndex, nsample, false);
mincdp= this->_maxminInt(cdpIndex, nsample, false); this->_maxCDPIndex = maxcdp.value; // 存为当前类对象的属性
this->_minCDPIndex = mincdp.value;
this->_maxLineIndex = maxline.index;
this->_minLineIndex = minline.index; this->_IncCDP = incCDP; // 赋值
this->_IncLine = incLine; this->_ncdp = (this->_maxCDPIndex - this->_minCDPIndex) / (this->_IncCDP) + 1; // 计算线数、道数
this->_nline = (this->_maxLineIndex-this->_minLineIndex)/ (this->_IncLine) + 1; // alloc2为各变量分配二维数组
// alloc2int、alloc2float与alloc2bool相似,均由alloc2衍生,而alloc2()返回一个void **ptr
this->_sliceIndexdataMatrix = alloc2int(this->_ncdp,this->_nline);
this->_cdpIndexMatrix = alloc2int(this->_ncdp, this->_nline);
this->_lineIndexMatrix= alloc2int(this->_ncdp, this->_nline);
this->_Xmatrix= alloc2float(this->_ncdp, this->_nline);
this->_Ymatrix = alloc2float(this->_ncdp, this->_nline);
this->_TimeMatrix= alloc2float(this->_ncdp, this->_nline);
this->_ValueMatrix= alloc2float(this->_ncdp, this->_nline);
this->_flagMatrix = alloc2bool(this->_ncdp, this->_nline); // flagMatrix为bool型 // 初始化
memset(this->_sliceIndexdataMatrix[0],0,(_ncdp)*(_nline)*sizeof(int));
memset(this->_cdpIndexMatrix[0], 0, (_ncdp)*(_nline) * sizeof(int));
memset(this->_lineIndexMatrix[0], 0, (_ncdp)*(_nline) * sizeof(int));
memset(this->_Xmatrix[0], 0, (_ncdp)*(_nline) * sizeof(float));
memset(this->_Ymatrix[0],0, (_ncdp)*(_nline) * sizeof(float));
memset(this->_TimeMatrix[0], 0, (_ncdp)*(_nline) * sizeof(float));
memset(this->_ValueMatrix[0],0, (_ncdp)*(_nline) * sizeof(float));
memset(this->_flagMatrix[0],0, (_ncdp)*(_nline) * sizeof(bool)); // 遍历采样点
for(int isample = 0; isample < nsample;isample++){ // 以左上角(cdp[0],line[0])为起点(0,0)换算相对索引
index_Xtemp = (cdpIndex[isample]-_minCDPIndex)/ _IncCDP;
index_Ytemp= (lineIndex[isample] - _minLineIndex) / _IncLine; // 如果这个点落在划定的矩形阵列内
if((index_Xtemp>=0)&&(index_Xtemp<_ncdp)&&(index_Ytemp>=0)&&(index_Ytemp<_nline)){ // 在相应的Matrix上填入该点数据
this->_sliceIndexdataMatrix[index_Ytemp][index_Xtemp] = slice[isample];
this->_cdpIndexMatrix[index_Ytemp][index_Xtemp] = cdpIndex[isample];
this->_lineIndexMatrix[index_Ytemp][index_Xtemp] = lineIndex[isample];
this->_Xmatrix[index_Ytemp][index_Xtemp] = X[isample];
this->_Ymatrix[index_Ytemp][index_Xtemp] = Y[isample];
this->_TimeMatrix[index_Ytemp][index_Xtemp] = time[isample];
this->_ValueMatrix[index_Ytemp][index_Xtemp] = value[isample]; this->_flagMatrix[index_Ytemp][index_Xtemp] = true; // true--->data exist }//end if((index_Xtemp>=0)&&(index_Xtemp<_ncdp)&&(index_Ytemp>=0)&&(index_Ytemp<_nline)) }//end for(int isample = 0; isample < nsample;isample++) return true;
}

(2) 求最大、最小坐标

MaxminResultInt SliceDataMatrix::_maxminInt(int *dataInput, int n, bool flag){

    // 输入为一个

	int value=dataInput[0];
int index=0; MaxminResultInt maxmin; if(flag==true){ for(int i= 0; i<n; i++){
if(dataInput[i] >value){
value = dataInput[i];
index = i;
}//end if(dataInput[i] >= value)
}//end for(int i= 0; i<n; i++) }else{ for (int i = 0; i < n; i++) {
if (dataInput[i] < value) {
value = dataInput[i];
index = i;
}//end if(dataInput[i] >= value)
}//end for(int i= 0; i<n; i++) }//end if(flag==true) maxmin.value = value;
maxmin.index = index; return maxmin; }

(3) get()方法

float** SliceDataMatrix::getTimeMatrix(){
return this->_TimeMatrix;
} float** SliceDataMatrix::getValueMatrix(){
return this->_ValueMatrix;
}

2.3 定义写入函数

写入函数接收一个二维数组,并将这个二维数组与其线道号匹配,将其以列表的形式输出到文本文件中。

bool SliceDataMatrix::writeData(float **dataoutput, int nx, int ny, const char *filenameoutput) {
//新建一个输出文件指针
FILE *fpoutput = fopen(filenameoutput, "wt"); if ((nx!=this->_ncdp)||(ny!=this->_nline))
{
return false;
}
//先写入表头
fprintf(fpoutput, "index line cdp X Y time value\n"); for (int iy = 0; iy < this->_nline; iy++) { //遍历线道号
for (int ix = 0; ix < this->_ncdp; ix++) {
if (this->_flagMatrix[iy][ix]==true) { //在有值的地方进行写入操作
fprintf(fpoutput, "%d %d %d %f %f %f %f\n",
this->_sliceIndexMatrix[iy][ix], this->_lineIndexMatrix[iy][ix],
this->_cdpIndexMatrix[iy][ix], this->_XMatrix[iy][ix],
this->_YMatrix[iy][ix],this->_TimeMatrix[iy][ix], dataoutput[iy][ix]);
}// end if (this->_flagMatrix[iy][ix]==true)
}// end for (int ix = 0; ix < this->_ncdp; ix++) //显示进度
if (iy%1000==0) {
printf("writelineNum=%d\n", iy); }//end if (iy % 1000 == 0) }// end for (int iy = 0; iy < this->_nline; iy++) { fclose(fpoutput); //关闭写入指针
return true;
}

3 编写主函数main.cpp

int main(int argc, char *argv) {

	SliceDataIO *slicedata = readSliceData("W11.slice");

	SliceDataMatrix *slicematrix = new SliceDataMatrix();	//新建一个切片阵列对象
slicematrix->setData(slicedata, 1, 1); //从读到的切片中赋值 int ncdp = slicematrix->getNumCdp(); //获取线道号
int nline = slicematrix->getNumLine(); float **dataOutput = alloc2float(ncdp, nline); //创建空的二维数组
memset(dataOutput[0], 0, ncdp*nline * sizeof(float)); //分配内存 float **dataInput = slicematrix->getValueMatrix(); //获取属性值阵列 slicematrix->writeData(dataInput, ncdp, nline, "output.txt"); //调用类的写方法
delete slicedata;
return 0;
}

运行主函数之后,程序将读入W11.slice,并将切片中的属性数据写至output.txt中。

完整代码

I SliceDataMatrix.h

/**********************************************************************

 * Copyright(C) 2018,Company All Rights Reserved
*
* @file : SliceDataMatrix.h
*
* @brief : 实现切片数据向二维阵列的转换
*
* @version : 1.0
*
* @author : Fan XinRan
*
* @date : 2022/2/8 星期二
*
* Others :
**********************************************************************/ #pragma once
#include"SliceDataIO.h" //SliceDataIO.h中包含了需要的标准库,全局常量等 typedef struct MaxminResultInt{
int value;
int index;
} MaxminResultInt; class SliceDataMatrix
{
public:
SliceDataMatrix();
~SliceDataMatrix();
//set方法
bool setData(SliceDataIO *datainput,int incLine,int incCDP);
//get方法
float **getTimeMatrix();
float **getValueMatrix();
bool **getFlagMatrix();
int getNumLine();
int getNumCdp();
//write方法
bool writeData(float **dataoutput,int nx,int ny,const char *filenameoutput);
private: int _nline;
int _ncdp;
int _IncLine;
int _IncCDP; int _maxLineIndex;
int _minLineIndex;
int _maxCDPIndex;
int _minCDPIndex; int **_sliceIndexMatrix;
int **_lineIndexMatrix;
int **_cdpIndexMatrix;
float **_XMatrix;
float **_YMatrix;
float **_TimeMatrix;
float **_ValueMatrix; bool **_flagMatrix; //用于判别该点是否有值 //计算最大最小点的函数,输入是一个一维数组,以及采样点数与最大/最小切换
MaxminResultInt _maxminint(int *dataInput, int n, bool flag);
};

II SliceDataMatrix.cpp

#include "SliceDataMatrix.h"

SliceDataMatrix::SliceDataMatrix(){	// 构造函数

	this->_IncCDP = 0;		// 采样间隔
this->_IncLine = 0;
this->_ncdp = 0; // 总线、道数
this->_nline = 0; this->_minCDPIndex = 0;
this->_maxCDPIndex = 0;
this->_minLineIndex = 0;
this->_maxLineIndex = 0; this->_sliceIndexdataMatrix = NULL; // 切片属性的二维阵列
this->_cdpIndexMatrix = NULL;
this->_lineIndexMatrix = NULL;
this->_TimeMatrix = NULL;
this->_ValueMatrix = NULL;
this->_Xmatrix = NULL;
this->_Ymatrix = NULL; this->_flagMatrix = NULL; // 标识矩阵 } SliceDataMatrix::~SliceDataMatrix(){ // 析构函数 if(this->_sliceIndexdataMatrix!=NULL){
free2int(_sliceIndexdataMatrix);
this->_sliceIndexdataMatrix = NULL;
} if(this->_cdpIndexMatrix!=NULL){
free2int(_cdpIndexMatrix);
this->_cdpIndexMatrix = NULL;
} if (this->_lineIndexMatrix != NULL) {
free2int(_lineIndexMatrix);
this->_lineIndexMatrix = NULL;
} if (this->_Xmatrix != NULL) {
free2float(_Xmatrix);
this->_Xmatrix = NULL;
} if (this->_Ymatrix != NULL) {
free2float(_Ymatrix);
this->_Ymatrix = NULL;
} if (this->_TimeMatrix != NULL) {
free2float(_TimeMatrix);
this->_TimeMatrix = NULL;
} if (this->_ValueMatrix != NULL) {
free2float(_ValueMatrix);
this->_ValueMatrix = NULL;
} if (this->_flagMatrix != NULL) {
free2bool(_flagMatrix);
this->_flagMatrix = NULL;
}
} // set method
bool SliceDataMatrix::setData(SliceDataIO *datainput, int incLine, int incCDP){ if(datainput==NULL){ // 判空
return false;
} if((incLine<0)||(incCDP<0)){ // 判定输入的采样间隔是否合法
return false;
} int *slice = datainput->getSliceIndex(); // 通过SliceDataIO类中的get()方法获取各类属性值
int *lineIndex = datainput->getLineIndex();
int *cdpIndex = datainput->getCDPIndex();
float *X = datainput->getX();
float *Y = datainput->getY();
float *time = datainput->getTime();
float *value = datainput->getValue(); int nsample = datainput->getSampleNum(); int index_Xtemp = 0; // 线、道号在阵列中的重新定位
int index_Ytemp = 0; MaxminResultInt maxline; // 针对切片的4个角点,新建四个结构体变量
MaxminResultInt maxcdp;
MaxminResultInt minline;
MaxminResultInt mincdp; maxline = this->_maxminInt(lineIndex, nsample, true); // 计算出最大/最小的值、索引
maxcdp = this->_maxminInt(cdpIndex, nsample, true);
minline= this->_maxminInt(lineIndex, nsample, false);
mincdp= this->_maxminInt(cdpIndex, nsample, false); this->_maxCDPIndex = maxcdp.value; //存到当前类对象
this->_minCDPIndex = mincdp.value;
this->_maxLineIndex = maxline.value;
this->_minLineIndex = minline.value; this->_IncCDP = incCDP;
this->_IncLine = incLine; this->_ncdp = (this->_maxCDPIndex - this->_minCDPIndex) / (this->_IncCDP) + 1; // 计算总线、道数
this->_nline = (this->_maxLineIndex-this->_minLineIndex)/ (this->_IncLine) + 1; // alloc2为各属性分配二维空间
this->_sliceIndexdataMatrix = alloc2int(this->_ncdp,this->_nline);
this->_cdpIndexMatrix = alloc2int(this->_ncdp, this->_nline);
this->_lineIndexMatrix= alloc2int(this->_ncdp, this->_nline);
this->_Xmatrix= alloc2float(this->_ncdp, this->_nline);
this->_Ymatrix = alloc2float(this->_ncdp, this->_nline);
this->_TimeMatrix= alloc2float(this->_ncdp, this->_nline);
this->_ValueMatrix= alloc2float(this->_ncdp, this->_nline);
this->_flagMatrix = alloc2bool(this->_ncdp, this->_nline); // 初始化
memset(this->_sliceIndexdataMatrix[0],0,(_ncdp)*(_nline)*sizeof(int));
memset(this->_cdpIndexMatrix[0], 0, (_ncdp)*(_nline) * sizeof(int));
memset(this->_lineIndexMatrix[0], 0, (_ncdp)*(_nline) * sizeof(int));
memset(this->_Xmatrix[0], 0, (_ncdp)*(_nline) * sizeof(float));
memset(this->_Ymatrix[0],0, (_ncdp)*(_nline) * sizeof(float));
memset(this->_TimeMatrix[0], 0, (_ncdp)*(_nline) * sizeof(float));
memset(this->_ValueMatrix[0],0, (_ncdp)*(_nline) * sizeof(float));
memset(this->_flagMatrix[0],0, (_ncdp)*(_nline) * sizeof(bool)); for(int isample = 0; isample < nsample;isample++){ index_Xtemp = (cdpIndex[isample]-_minCDPIndex)/ _IncCDP; // 以左上角为(0,0)转换后的索引
index_Ytemp= (lineIndex[isample] - _minLineIndex) / _IncLine; if((index_Xtemp>=0)&&(index_Xtemp<_ncdp)&&(index_Ytemp>=0)&&(index_Ytemp<_nline)){ // 如果该点落在矩形阵列范围内 this->_sliceIndexdataMatrix[index_Ytemp][index_Xtemp] = slice[isample]; // 相应的Matrix中填入该点数据
this->_cdpIndexMatrix[index_Ytemp][index_Xtemp] = cdpIndex[isample];
this->_lineIndexMatrix[index_Ytemp][index_Xtemp] = lineIndex[isample];
this->_Xmatrix[index_Ytemp][index_Xtemp] = X[isample];
this->_Ymatrix[index_Ytemp][index_Xtemp] = Y[isample];
this->_TimeMatrix[index_Ytemp][index_Xtemp] = time[isample];
this->_ValueMatrix[index_Ytemp][index_Xtemp] = value[isample]; this->_flagMatrix[index_Ytemp][index_Xtemp] = true; // true--->data exist }//end if((index_Xtemp>=0)&&(index_Xtemp<_ncdp)&&(index_Ytemp>=0)&&(index_Ytemp<_nline)) }//end for(int isample = 0; isample < nsample;isample++) return true;
}
// 计算切片角点的最大、最小 Line/CDP
MaxminResultInt SliceDataMatrix::_maxminInt(int *dataInput, int n, bool flag){ int value=dataInput[0];
int index=0; MaxminResultInt maxmin; if(flag==true){ // flag=true---> max value/index for(int i= 0; i<n; i++){
if(dataInput[i] >value){
value = dataInput[i];
index = i;
}//end if(dataInput[i] >= value)
}//end for(int i= 0; i<n; i++) }else{ // else---> min value/index for (int i = 0; i < n; i++) {
if (dataInput[i] < value) {
value = dataInput[i];
index = i;
}//end if(dataInput[i] >= value)
}//end for(int i= 0; i<n; i++) }//end if(flag==true) maxmin.value = value;
maxmin.index = index; return maxmin; } // -------------------get()方法------------------
float** SliceDataMatrix::getTimeMatrix(){
return this->_TimeMatrix;
} float** SliceDataMatrix::getValueMatrix(){
return this->_ValueMatrix;
} bool** SliceDataMatrix::getFlagMatrix(){
return this->_flagMatrix;
} int SliceDataMatrix::getNumLine(){
return this->_nline;
} int SliceDataMatrix::getNumCdp(){
return this->_ncdp;
} // write data to outputfile
bool SliceDataMatrix::writeData(float **dataoutput, int nx, int ny, const char *filenameoutput) {
//新建一个输出文件指针
FILE *fpoutput = fopen(filenameoutput, "wt"); if ((nx!=this->_ncdp)||(ny!=this->_nline))
{
return false;
}
//先写入表头
fprintf(fpoutput, "index line cdp X Y time value\n"); for (int iy = 0; iy < this->_nline; iy++) {
for (int ix = 0; ix < this->_ncdp; ix++) {
if (this->_flagMatrix[iy][ix]==true) {
fprintf(fpoutput, "%d %d %d %f %f %f %f\n", this->_sliceIndexMatrix[iy][ix], this->_lineIndexMatrix[iy][ix],
this->_cdpIndexMatrix[iy][ix], this->_XMatrix[iy][ix], this->_YMatrix[iy][ix],
this->_TimeMatrix[iy][ix], dataoutput[iy][ix]);
}// end if (this->_flagMatrix[iy][ix]==true)
}// end for (int ix = 0; ix < this->_ncdp; ix++) //显示进度
if (iy%1000==0) {
printf("writelineNum=%d\n", iy); }//end if (iy % 1000 == 0) }// end for (int iy = 0; iy < this->_nline; iy++) { fclose(fpoutput);
return true;
}

III main.cpp

#include "SliceDataIO.h"
#include"SliceDataMatrix.h" int main(int argc, char *argv) { SliceDataIO *slicedata = readSliceData("W11.slice"); SliceDataMatrix *slicematrix = new SliceDataMatrix();
slicematrix->setData(slicedata, 1, 1); int ncdp = slicematrix->getNumCdp();
int nline = slicematrix->getNumLine(); float **dataOutput = alloc2float(ncdp, nline);
memset(dataOutput[0], 0, ncdp*nline * sizeof(float)); float **dataInput = slicematrix->getTimeMatrix();
//bool **flag = slicematrix->getFlagMatrix();
//writeSliceData("W11.txt", slicedata);
//writeSliceData("W11copy.slice", slicedata); slicematrix->writeData(dataInput, ncdp, nline, "output.txt");
delete slicedata;
return 0;
}

附录

void*

void被翻译为“无类型”,相应的void*为“无类型指针”。

  • 当函数不需要返回值时,必须使用void限定,如:void func(int a,char *b)
  • 当函数不允许接受参数时,同样必须使用void加以限定,如:int func(void)

void指针的使用规则:

1.void指针可以指向任意类型的数据,即可以用任意类型的指针对void指针赋值。如:

int *a;
void *p;
p = a; // int * 型的a可以直接赋值给void * 型的p

2.如果要将void指针p赋值给其他类型的指针,则需要强制类型转换,如:a = (int *)p

在上文中提及的alloc2()函数,其定义为:

void **alloc2(size_t n1, size_t n2, size_t size)
{
size_t i2;
void **p; ... return p; // p是一个void ** 型的指针变量
}

alloc2int()函数的定义为:

int **alloc2int(size_t n1, size_t n2)
{
return (int**)alloc2(n1, n2, sizeof(int)); // 使用(int **)强制类型转换
}

同理,可以依据此规则写出alloc2bool()

bool **alloc2bool(size_t n1, size_t n2)
{
return (bool**)alloc2(n1, n2, sizeof(bool)); // 使用(bool **)强制类型转换
}

C/C++将切片数据列表转换为二维数组形式的更多相关文章

  1. 将List转换为二维数组(result)

    result的数据结构为List<Map<String,Object>> //将List转换为二维数组String[][] String[][] z = new String[ ...

  2. Java读取excel指定sheet中的各行数据,存入二维数组,包括首行,并打印

    1. 读取 //读取excel指定sheet中的各行数据,存入二维数组,包括首行 public static String[][] getSheetData(XSSFSheet sheet) thro ...

  3. 【ruby题目】以|为分割点,将arr转换为二维数组

    #以|为分割点,将arr转换为二维数组 arr = ['] tmp = [] tmp2 = [] for x in arr tmp << x if x != '|' tmp2.push A ...

  4. js一维数组转换为二维数组

    function arrTrans(num, arr) { // 一维数组转换为二维数组 const iconsArr = []; // 声明数组 arr.forEach((item, index) ...

  5. C语言一维数组转换为二维数组

    一维转二维代码示例: #include <stdio.h> #include <stdlib.h> #define ROW 3 #define COL 2 int main(i ...

  6. C语言批量数据到动态二维数组

    上一篇文章将文件读取放到静态创建的二维数组中,可是结合网络上感觉到今天的DT时代,这样批量大量读取一个上百行的数据,分配的内存是否可能由于大量的数据而产生溢出呢,近期一直研究里malloc函数.通过它 ...

  7. List<T>转换为二维数组

    public <T> Object[][] toArrays(List<T> data){ Object[][] o=new Object[data.size()][20]; ...

  8. 触电JavaScript-如何将json 二维数组转换为 JSON object

    最近因为项目中使用的是 ActiveReports .Net 产品,因为他们最近新出了  ActiveReports JS 版本,所以内心有点痒痒,想试试这个纯前端版本报表控件到底如何,毕竟我们项目有 ...

  9. PHP二维数组提取函数----把不需要的数据剔除

    首先说明一些这个函数的应用场景,比如说你得到的数据是个二维数组,里面的很多成员其实是不必要的,比如说api调用后不必要给别人返回一些用不到的垃圾数据吧,如下是代码. <?php /* * del ...

  10. ***php解析JSON二维数组字符串(json_decode函数第二个参数True和False的区别)

    客户端的请求体中的数据:[{"msg_id": 1, "msg_status": "HAS_READ" }, { "msg_id& ...

随机推荐

  1. 一文速通 Python 并行计算:05 Python 多线程编程-线程的定时运行

    一文速通 Python 并行计算:05 Python 多线程编程-线程的定时运行 摘要: 本文主要讲述了 Python 如何实现定时任务,主要有四种方式:通过 threading.Timer 类.通过 ...

  2. 《机器人SLAM导航核心技术与实战》第1季:第2章_C++编程范式

    <机器人SLAM导航核心技术与实战>第1季:第2章_C++编程范式 视频讲解 [第1季]2.第2章_C++编程范式-视频讲解 [第1季]2.1.第2章_C++编程范式-C++工程的组织结构 ...

  3. nbhh的泛型:TDictionary

    type TCity = class Country: String; Latitude: Double; Longitude: Double; end; const EPSILON = 0.0000 ...

  4. Codeforces Round 952 (Div. 4)

    知识点模块 1.一个正方体x,y,z里面可以放多少个边长为a,b,c的长方体 ans=(x-a+1)*(y-b+1)*(z-c+1) 题解模块 A.Creating Words 交换两个字母的首字母即 ...

  5. RAGflow搭建text-to-sql的AI研发助手

    一.概述 text-to-sql 技术允许用户通过自然语言提问,系统自动将其转换为 SQL 语句并执行,大大降低了数据查询的门槛,提高了工作效率. text-to-sql 技术在数据分析.智能客服.数 ...

  6. C 冒泡排序和选择排序

    冒泡排序 理论概念: 从第一个数开始,将相邻的两个数比较,第一个数和第二个数比较,比如说是从小到大的排序,要是后面的数比前面的小则交换两个的位置,这样第一轮比较基数后最大的数就到了最后面,接着进行第二 ...

  7. 能详细地讲讲stm32该怎么学吗?

    作为一个在嵌入式领域摸爬滚打了好几年的老兵,我想分享一下我学习STM32的心路历程和方法论.坦白说,刚开始接触STM32时,我也是一脸懵逼.机械专业毕业的我转行做嵌入式,第一份工作被调剂到电子部门,实 ...

  8. RocketMQ的Consumer是如何消费消息的

    Rocketmq提供了两种主要的消费模式:推送式消费(Push Consumer)和 拉取式消费(Pull Consumer) 一.Consumer消费消息的基本流程 1.实例化Consumer:创建 ...

  9. 判断返回值长度(比如是否为空),执行后续步骤(if..else、len的用法)

    爬基金数据,净值因涨跌不同,对应的元素路径也不会一样 比如当天是涨的时候,涨跌元素的class信息为"<span class="fix_dwjz  bold ui-color ...

  10. sql学习day3——case when的使用

    1,当前表 course_master                                                            open_course           ...