本文介绍了转换C ++实现矢量在c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经用C ++编写以下code,但发现我必须把它转换成C.我不是C甚至C ++程序员,请帮助。
有人可以帮我改这个方法为C指令,具体的实施载体,以下不会编译我已删除的复杂性,以保持它的简单。谢谢期待。
__ declspec(dllexport)的的std ::矢量<&MY_STRUCT GT; WINAPI ABC(字符* strVal)
{
MY_STRUCT F;
的std ::矢量<&MY_STRUCT GT;列表=的std ::矢量<&MY_STRUCT GT;();
而(* DDDD)
{/ *做每个功能下面的许可证文件* / f.attrib_num = fi.attrib_num;
f.attrib_lic = fi.attrib_lic; list.push_back(F);
} / *结束时(CONF)* / DDDD ++;
的printf(\\ n);} / *结束时(* DDDD)* /返回FLIST;}
解决方案
下面是C.你可以把它适应你的结构结构的动态数组实现(也使用);我曾经张贴在code审查之前
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;typedef结构
{
INT ID;
字符*名称;
}学生;//结构的数组
typedef结构
{
学生*阵列;
为size_t使用;
为size_t的大小;
}数组;无效initArray(数组*一,为size_t INITIALSIZE)
{
INT I = 0;
//分配初始空间
A->阵=的malloc(INITIALSIZE * sizeof的(学生)); A->作为= 0; //使用无元素
A->大小= INITIALSIZE; //元素可用NR //初始化数组的所有值设置为0
对于(i = 0; I< INITIALSIZE;我++)
{
memset的(安培; A->阵[I],0,sizeof的(学生));
}
}//添加元素的数组
无效的addElement(数组*一,学生元件)
{
INT I = 0;
如果(A->作为== A->大小)
{
A->大小* = 2;
A->阵=的realloc(A->阵,A->大小* sizeof的(学生)); //初始化重新分配阵列的最后/新元素
对于(I = A->作为; I< A->大小;我++)
{
memset的(安培; A->阵[I],0,sizeof的(学生));
}
} //复制名称
A->阵[A->作为]。名称=(字符*)malloc的(strlen的(element.name)+ 1);
的strcpy(A->阵[A->作为] .name和element.name); //复制ID
A->阵[A->作为] .ID = element.ID; A->作为++;
}无效freeArray(阵列*一)
{
INT I = 0;
//自由每个数组元素的所有名称第一个变量
对于(i = 0; I< A->作为;我++)
{
免费(A->阵列[我]。名称);
A->阵列[我]。名称= NULL;
} //现在释放数组
免费(A->数组);
A->阵= NULL; A->作为= 0;
A->大小= 0;
}INT主(INT ARGC,为const char * argv的[])
{ 阵列;
学生X,Y,Z; x.ID = 20;
x.name = malloc的(strlen的(stud1)+ 1);
的strcpy(x.namestud1); y.ID = 30;
y.name = malloc的(strlen的(STUDENT2)+ 1);
的strcpy(y.nameSTUDENT2); z.ID = 40;
z.name = malloc的(strlen的(学生三)+ 1);
的strcpy(z.name,学生三); //初始化数组,不要忘了
initArray(&放大器;一,5); //添加元素
的addElement(&放大器; A,X);
的addElement(&放大器; A,Y);
的addElement(&放大器; A,Z); //打印元素
的printf(%d个\\ N,a.array [0] .ID);
的printf(%S \\ n,a.array [0]。名称);
的printf(%d个\\ N,a.array [1] .ID);
的printf(%S \\ n,a.array [1]。名称);
的printf(%d个\\ N,a.array [2] .ID);
的printf(%S \\ n,a.array [2]。名称);
//自由阵
//不要忘了
freeArray(&放大器;一个); 免费(x.name);
免费(y.name);
免费(z.name); 返回0;
}
I have written the following code in C++, however found out that I have to convert it in C. I am not C or even C++ programmer, please help.
Can someone help me change this method to C directives, specifically vector implementation, following will not compile I have removed complexity to keep it simple. Thanks in anticipation.
__declspec(dllexport) std::vector<MY_STRUCT> WINAPI ABC(char *strVal)
{
MY_STRUCT f;
std::vector<MY_STRUCT> list = std::vector<MY_STRUCT>();
while (*dddd)
{ /*do the following for every feature in license file*/
f.attrib_num = fi.attrib_num;
f.attrib_lic = fi.attrib_lic;
list.push_back(f);
} /* end while(conf) */
dddd++;
printf("\n");
} /* end while (*dddd) */
return flist;
}
解决方案
Here is implementation (also usage) of dynamic array of structs in C. You can adapt it to your structure; I used to post it on code review before
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int ID;
char * name;
} Student;
// array of structs
typedef struct
{
Student *array;
size_t used;
size_t size;
} Array;
void initArray(Array *a, size_t initialSize)
{
int i = 0;
// Allocate initial space
a->array = malloc(initialSize * sizeof(Student));
a->used = 0; // no elements used
a->size = initialSize; // available nr of elements
// Initialize all values of the array to 0
for(i = 0; i<initialSize; i++)
{
memset(&a->array[i],0,sizeof(Student));
}
}
// Add element to array
void addElement(Array *a, Student element)
{
int i = 0;
if (a->used == a->size)
{
a->size *= 2;
a->array = realloc(a->array, a->size * sizeof(Student));
// Initialize the last/new elements of the reallocated array
for(i = a->used; i<a->size; i++)
{
memset(&a->array[i],0,sizeof(Student));
}
}
// Copy name
a->array[a->used].name = (char*)malloc(strlen(element.name) + 1);
strcpy(a->array[a->used].name, element.name);
// Copy ID
a->array[a->used].ID=element.ID;
a->used++;
}
void freeArray(Array *a)
{
int i = 0;
// Free all name variables of each array element first
for(i=0; i<a->used; i++)
{
free(a->array[i].name);
a->array[i].name=NULL;
}
// Now free the array
free(a->array);
a->array = NULL;
a->used = 0;
a->size = 0;
}
int main(int argc, const char * argv[])
{
Array a;
Student x,y,z;
x.ID = 20;
x.name=malloc(strlen("stud1") + 1);
strcpy(x.name,"stud1");
y.ID = 30;
y.name=malloc(strlen("student2") + 1);
strcpy(y.name,"student2");
z.ID = 40;
z.name=malloc(strlen("student3") + 1);
strcpy(z.name,"student3");
// Init array, don't forget
initArray(&a, 5);
// Add elements
addElement(&a, x);
addElement(&a, y);
addElement(&a, z);
// Print elements
printf("%d\n", a.array[0].ID);
printf("%s\n", a.array[0].name);
printf("%d\n", a.array[1].ID);
printf("%s\n", a.array[1].name);
printf("%d\n", a.array[2].ID);
printf("%s\n", a.array[2].name);
// Free array
// don't forget
freeArray(&a);
free(x.name);
free(y.name);
free(z.name);
return 0;
}
这篇关于转换C ++实现矢量在c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!