将C ++向量转换为C

时间:2019-02-23 13:56:08

标签: c++ c arrays linked-list

我正在尝试转换向量vectorName; C到C的实现 实际上,我必须在C中使用其某些功能,例如vectorName.push_back(string),但是我该怎么做却没有适当的实现,并且有什么方法可以使用c ++库并将其修补到C代码中,这是我的想法

typedef struct vector_ {
      void** data;
      int size;
      int count;
} vector;

void vector_add(vector*, void*);
void vector_add(vector *v, void *e)
{
    if (v->size == 0) {
        v->size = 10;
        v->data = malloc(sizeof(void*) * v->size);
        memset(v->data, '\0', sizeof(void*) * v->size);
    }

    if (v->size == v->count) {
        v->size *= 2;
        v->data = realloc(v->data, sizeof(void*) * v->size);
    }

    v->data[v->count] = e;
    v->count++;
}

2 个答案:

答案 0 :(得分:3)

一个例子:

#include <stdio.h>
#include <stdlib.h>

#define DEF_VECTOR(type) \
typedef struct vector_##type { \
  type * data; \
  size_t alloc_size; \
  size_t size; \
} vector_##type; \
\
void init_vector_##type(vector_##type * vector) \
{ \
  vector->data = NULL; \
  vector->alloc_size = vector->size = 0; \
} \
\
void clear_vector_##type(vector_##type * vector) \
{ \
  if (vector->data != NULL) {\
    free(vector->data); \
    init_vector_##type(vector);  \
  } \
} \
\
void push_back_vector_##type(vector_##type * vector, type value) \
{ \
  if (vector->size == vector->alloc_size) { \
    vector->alloc_size = (vector->alloc_size == 0) ? 16 : vector->alloc_size * 2; \
    vector->data = realloc(vector->data, vector->alloc_size * sizeof(type)); \
     \
    if (vector->data == NULL) { \
      /* do what you want */ \
    } \
  } \
  vector->data[vector->size++] = value; \
} \
\
type at_vector_##type(vector_##type * vector, size_t index) \
{ \
  if (index >= vector->size) { \
    /* do what you want */ \
  } \
  return vector->data[index]; \
}

DEF_VECTOR(int)

int main()
{
  vector_int v;

  init_vector_int(&v);
  push_back_vector_int(&v, 123);
  push_back_vector_int(&v, 456);
  printf("%d %d\n", at_vector_int(&v, 0), at_vector_int(&v, 1));
  printf("%d %d\n", v.data[0], v.data[1]);
  clear_vector_int(&v);
  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra v.c
pi@raspberrypi:/tmp $ ./a.out
123 456
123 456

valgrind

下执行
pi@raspberrypi:/tmp $ valgrind ./a.out
==11108== Memcheck, a memory error detector
==11108== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==11108== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==11108== Command: ./a.out
==11108== 
123 456
123 456
==11108== 
==11108== HEAP SUMMARY:
==11108==     in use at exit: 0 bytes in 0 blocks
==11108==   total heap usage: 2 allocs, 2 frees, 1,088 bytes allocated
==11108== 
==11108== All heap blocks were freed -- no leaks are possible
==11108== 
==11108== For counts of detected and suppressed errors, rerun with: -v
==11108== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)

当然,您不必忘记调用init_vector_xx,并且如果需要执行clear_vector_xx并使用宏定义全部内容,则在执行时出错时将无济于事,因为您没有该行号码等

在这里,我将所有内容放入唯一的宏中,实际上最好有一个宏来定义 struct 并声明函数,而另一个宏来定义函数,以便能够放入标题和来源中的内容。

  

有什么方法可以使用c ++库并将其修补为C代码

不确定是否可以回答该问题。当然,您可以从它们的定义中获得一些想法,因为它们的实施很好,但是您也冒着被不需要翻译的复杂性淹没的风险。

但是,如果您可以使用C ++,请不要使用C;-)

答案 1 :(得分:0)

如果您想要类似于std::vector<char>的东西,那么等效的push_back(char ch)解决方案建议可能是:

#include <stdlib.h>
#include <stdio.h>

typedef struct vectorOfChar vector;
struct vectorOfChar {
    char *start;
    char *finish;
    char *end_of_storage;
};

vector vector_construct(void) {
    vector v = {0};
    return v;
}

void vector_destruct(vector *const vec) {
    free(vec->start);
}

char *vector_data(vector *const vec) {
    return vec->start;
}

void vector_push_back(vector *const vec, char const ch) {
    if (vec->finish != vec->end_of_storage) {
        *vec->finish = ch;
        ++vec->finish;
    } else {
        int const size = vec->finish - vec->start;
        int capacity = vec->end_of_storage - vec->start;
        capacity++;
        capacity *= 3;
        capacity /= 2;
        char *const new_area = realloc(vec->start, capacity);
        if (new_area == NULL) {
            // define how to handle out-of-memory
            exit(EXIT_FAILURE);
        }
        vec->start = new_area;
        vec->finish = new_area + size;
        vec->end_of_storage = new_area + capacity;
        vector_push_back(vec, ch);
    }
}

int main() {
    vector v = vector_construct();

    for (char c = 'a'; c <= 'z'; c++) {
        vector_push_back(&v, c);
    }
    vector_push_back(&v, '\0');

    printf("%s\n", vector_data(&v));

    vector_destruct(&v);

    return EXIT_SUCCESS;
}