如何在不使用任何库的情况下在C中重现Ruby的Array.compact()?

时间:2015-07-17 09:58:38

标签: c

我需要在C中使用此函数,它将字符串选项卡及其长度作为参数。它应该删除空的tab块(就像Ruby的Array.compact一样)并返回新标签的大小:

int     strtab_compact(char **tab, int length)
{
    ...
}

例如,如果我有:

tab[0] = "foo";
tab[1] = 0; // nul
tab[2] = "bar";
tab[3] = 0;
tab[4] = "baz";
i = strtab_compact(tab, 5) // tab should = ["foo"]["bar"]["baz"] and i = 3

我刚刚开始使用C(这是我的第二周),我有以下代码,但我完全被卡住了:

int strtab_compact(char **tab, int length)
{
    int i;
    int index;

    iterations = 0;
    i = 0;
    // First iteration to get how many valid entries we've got
    while (i < length)
    {
        if (tab[i])
            index++;
        i++;
    }
    // I'll need a second iteration to reorganize the tab and I have no idea how to delete the remaining slots
    i = 0;
    while (...)
    {
        ...
    }
    return (index);
}

2 个答案:

答案 0 :(得分:1)

您不需要两次迭代,但需要两个迭代器索引:i遍历数组,index仅在找到非空字符串时才会前进。然后,紧凑数组的长度是迭代后index的值:

int strtab_compact(char **tab, int length)
{
    int index = 0;
    int i;

    for (i = 0; i < length; i++) {
        if (tab[i]) tab[index++] = tab[i];
    }

    return index;
}

因为只删除了NULL个条目,所以如果删除的字符串在堆上分配,则不必担心会释放内存。 (在你的例子中,它们不是。我只是想提一下你必须注意你删除的元素会发生什么,因为压缩后对它们的访问会丢失。)

答案 1 :(得分:0)

int strtab_compact(char **tab, int length){
    int i, index;

    for(index = i = 0; i < length; ++i){
        if(tab[i] != NULL)
            tab[index++] = tab[i];
    }
    for(i = index; i < length; ++i)
        tab[i] = NULL;

    return index;
}