【问题标题】:Converting C++ implementation of vector in C在 C 中转换向量的 C++ 实现
【发布时间】:2015-01-16 20:31:13
【问题描述】:

我用C++编写了以下代码,但是发现我必须用C转换它。我不是C甚至C++程序员,请帮助。

谁能帮我把这个方法改成 C 指令,特别是向量实现,下面不会编译我已经删除了复杂性以保持简单。感谢期待。

__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;

}

【问题讨论】:

  • 你可以用realloc实现你自己的push_back。使向量成为一个结构,以便您可以将其当前和分配的大小与结构指针一起保留。
  • ddddfiflist 都没有我们可以看到的声明,所以这个函数的语义完全不清楚,这会妨碍任何重写它的尝试。

标签: c++ c vector implementation


【解决方案1】:

这是 C 中动态结构数组的实现(也是用法)。您可以根据自己的结构调整它;我以前曾在代码审查上发布过它

#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;
}

【讨论】:

  • 谢谢。我在代码行 a->array[i].version=NULL 和类似内容上收到错误“表达式必须是可修改的左值”。
  • @Warrior:你从哪里得到版本
  • 忽略版本,这只是我的实现,将光标悬停在a上时会显示错误。
  • @Warrior:现在检查编辑后的答案,做了一些小的改动。它编译:codepad.org/QjqW3jmh。正如我所说,你必须根据你的结构调整它
  • 我尝试编译代码。 a->array = malloc(initialSize * sizeof(Student));和 a->array = realloc(a->array, a->size * sizeof(Student));行没有编译,因为两者都没有返回任何东西,即它们是无效的
猜你喜欢
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 2017-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
相关资源
最近更新 更多