我有陣A[9]= {1,2,3,4,5,6,7,8,9}
,我需要刪除未通過2.代碼劃分的數字我試圖做的:C++數組不能得到正確的陣列
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;
}
但我得到這樣224466888
號。我需要做什麼來獲得2,4,6,8?
我需要刪除相同數組中的數字。
第一條評論:有意義的變量名使代碼更容易閱讀。 – Almo
爲什麼不使用更好的數據結構,比如'std :: vector'或'std :: list'? – crashmstr
@crashmstr因爲學習。 – molbdnilo