删除数组中的尾随零

时间:2017-04-21 17:41:11

标签: c++

我正在尝试创建一个不在数组中打印尾随零的程序 我的数组大小为23.我试图从for循环中的第23个位置开始,并在到达一个时结束for循环。然后我只打印到那个位置。我似乎无法做到这一点。有人可以帮我这个吗? 感谢

void removeTrailZero(int array[]) {
    int i = 0;

    for (i = 23; array[i] == 0; i--) {
        // printf("%d", i);
    }

    for (int x = 1; x < i + 1; x++) {
        printf("%d",array[x]);
    }
}

3 个答案:

答案 0 :(得分:2)

你可以做这样的事情

for (i = 22; i >= 0; --i)
{
    if (array[i] != 0)
        break;
}

然后在0和i(包括)之间打印数组

答案 1 :(得分:2)

现在,您的代码看起来像C,而不是C ++。至少假设标​​签是正确的,你真的想写C ++而不是C,我考虑使用标准库中的一些算法来处理至少部分工作。

标准库有<logger name="FileNotFound" ..>用于查找您关注的值。这适用于迭代器,而不是直接与底层存储一起使用。由于您想从头开始搜索,可以使用std::find_if找到正确的点:

reverse_iterator

当我们打印出数据时,我们希望从头到尾迭代前进 - 但这是一个反向器,所以我们需要调用它{{1获取一个前向迭代器,并将其作为我们想要打印的范围的结尾:

auto last = std::find_if(std::crbegin(array), std::crend(array), 
    [](auto i) { return i != 0; });

答案 2 :(得分:0)

如果您使用C ++ 14 -std=c++14进行编译,则可以使用答案的@JerryCoffin:

#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <string>

template <std::size_t SIZE>
void removeTrailZero(const std::array<int, SIZE>& array) {
    auto last = std::find_if(std::crbegin(array), std::crend(array),
        [](auto i) { return i != 0; });
    std::copy(std::cbegin(array), last.base(),
        std::ostream_iterator<int>(std::cout, " "));
}

int main() {
    std::array<int, 23> array1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, 0, 0, 0, 0, 0, 0};
    removeTrailZero(array1);
    std::cout << std::endl;
    std::array<int, 15> array2 = {1, 2, 3, 0, 0, 4, 5, 6, 0, 0, 0, 0, 0, 0};
    removeTrailZero(array2);
    std::cout << std::endl;
}

输出:

❯❯❯ g++ -std=c++14 ../test.cc -o test && ./test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 0 0 4 5 6

使用C ++ 11 -std=c++11进行编译时,您可以使用:

#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <string>

template <std::size_t SIZE>
void removeTrailZero(const std::array<int, SIZE>& array) {
    auto last = std::find_if(array.crbegin(), array.crend(),
        [](int i) { return i != 0; });
    std::copy(array.cbegin(), last.base(),
        std::ostream_iterator<int>(std::cout, " "));
}

int main() {
    std::array<int, 23> array1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, 0, 0, 0, 0, 0, 0};
    removeTrailZero(array1);
    std::cout << std::endl;
    std::array<int, 15> array2 = {1, 2, 3, 0, 0, 4, 5, 6, 0, 0, 0, 0, 0, 0};
    removeTrailZero(array2);
    std::cout << std::endl;
}

输出:

❯❯❯ g++ -std=c++11 ../test.cc -o test && ./test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 0 0 4 5 6

对于旧版本的C ++ 11,您可以使用:

#include <iostream>

void removeTrailZero(int array[]) {
    int i = 22;
    for (; i >= 0 && array[i] == 0; --i);
    for (int j = 0; j <= i; ++j) {
        std::cout << array[j] << " ";
    }
}

int main() {
    int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, 0, 0, 0, 0, 0, 0};
    removeTrailZero(array);
    std::cout << std::endl;
    int array2[] = {1, 2, 3, 0, 0, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    removeTrailZero(array2);
    std::cout << std::endl;
}

输出:

❯❯❯ g++ ../test.cc -o test && ./test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 0 0 4 5 6
  

注意:对于C ++ 11和C ++ 14,removeTrailZero是一个模板函数,可以接收不同大小的std::array。对于旧版本的C ++ 11 removeTrailZero,设计用于接收int[23]