简体   繁体   English

c ++数组无法获得正确的数组

[英]c++ array can't get the right array

I have Array A[9]= {1,2,3,4,5,6,7,8,9} and I need to delete the numbers which are not dividing by 2. The code I tried to do: 我有数组A[9]= {1,2,3,4,5,6,7,8,9}我需要删除不除以2的数字。我试图做的代码:

int main()
{
    int n;
    ifstream fd(Cdf);
    fd>>n; // read how many numbers are in the file.
    int A[n];
    for(int i = 0; i < n; i++)
    {
        fd >> A[i]; //read the numbers from file
    }
    for(int i = 0; i < n; i ++) // moving the numbers.
    {
        if(A[i] % 2 !=0)
        {
            for(int j = i; j < n; j++)
            {
                A[i] = A[i+1];
            }
        }
    }
    fd.close();
    return 0;
}

But I get numbers like 224466888 . 但我得到像224466888这样的224466888 what I need to do to get 2,4,6,8? 我需要做什么来获得2,4,6,8?

I need to delete numbers in the same array. 我需要删除同一个数组中的数字。

First you should use std::vector for dynamic size arrays. 首先,您应该将std :: vector用于动态大小的数组。 Second, for removing numbers that are even in a vector, you can do : 其次,要删除偶数在向量中的数字,您可以:

std::vector<int> inf = {12,0,5,6,8};
auto func = [](int i){return i % 2 != 0;}; 
inf.erase(std::remove_if(inf.begin(),inf.end(),func), inf.end());

EDIT : 编辑:

Ok, so you can still do this without std::vectors, but it will be uglier : 好的,所以你仍然可以在没有std :: vectors的情况下做到这一点,但它会更加丑陋:

#include <algorithm>

int res[] = {2,5,9,8,6,7};
int size = 6;
auto func = [](int i){return i % 2 != 0;};
int new_size = std::remove_if(res,res + size, func) - res;

All the data you want is in [0, new_size[ range, the other part of your array is now garbage. 你想要的所有数据都在[0,new_size [范围内,你的数组的另一部分现在是垃圾。

An array can't be used for your purpose. 数组不能用于您的目的。 It is allocated on stack and its size can't be changed dynamically (you can't change the size of an array in general, not only when it is allocated on stack). 它在堆栈上分配,其大小不能动态更改(通常不能更改数组的大小,不仅在堆栈上分配时)。

You could allocate a second array and keep reallocating it with realloc everytime you add a new element but that's not the good way to do it. 您可以分配第二个数组,并在每次添加新元素时使用realloc重新分配它,但这不是执行此操作的好方法。 You are working with C++ so just use a std::vector<int> and your problems will be solved: 您正在使用C ++,所以只需使用std::vector<int> ,您的问题就会得到解决:

std::vector<int> evenArray;
evenArray.reserve(sizeof(A)/sizeof(A[0])/2);

if (number is even) {
  evenArray.pushBack(number);
}

Mind that vector stores elements contiguously so this is legal: 请注意,矢量连续存储元素,因此这是合法的:

int *evenA = &evenArray[0];

For your inner for loop you should be referencing j , not i . 对于你的内部for循环,你应该引用j ,而不是i

for(int j = i; j < n - 1; j++)
{
    A[j] = A[j+1];
}

Otherwise, what's the point of creating j ? 否则,创建j的重点是什么?

Of course, this also means if you read the whole array back you will display all the characters that were shifted (which will just be equal to the last number). 当然,这也意味着如果你读回整个数组,你将显示所有被移位的字符(这将等于最后一个数字)。 So, you should probably keep track of the new length of the array and just iterate to that instead of the end of the array. 所以,你应该跟踪数组的新长度,然后迭代到那个而不是数组的末尾。

EDIT: 编辑:
In the inner for loop you need to loop to n - 1 otherwise when you have A[j + 1] it will go off the end of the array when you to change it, which may or may not give you a runtime error. 在内部for循环中,你需要循环到n - 1否则当你有A[j + 1]它会在你改变它时离开数组的末尾,这可能会或可能不会给你一个运行时错误。

Your removal loop is indexing with the wrong variable: 您的删除循环使用错误的变量进行索引:

for(int j = i; j < n; j++)
{
    A[i] = A[i+1];
}

You're using i , which doesn't change in the loop. 你正在使用i ,它在循环中不会改变。
Change it to j . 将其更改为j You also need to subtract one from the upper limit, as you'd step outside of the array otherwise when accessing A[j + 1] . 您还需要从上限中减去一个,因为当您访问A[j + 1]时,您将走出数组。

for(int j = i; j < n - 1; j++)
{
    A[j] = A[j + 1];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM