[英]C++ Splitting an array into 2 separate arrays
我需要編寫一個程序,它接受一個給定的數組,然后將它分成兩個獨立的數組,其中一個數組的元素是主數組的正元素,另一個元素是主數組的負元素。
在充分利用代碼之后,我在嘗試編譯時遇到了大約一百萬行錯誤。 我如何刪除三個動態分配的數組有問題嗎? 什么巨大的錯誤阻止編譯? 這是我的代碼:
#include <iostream>
using namespace std;
void count(int ARRAY[], int SIZE, int& NEG, int& POS);
void split(int ARRAY[], int SIZE, int& NEG_ARRAY, int NEG, int& POS_ARRAY, int POS);
void print_array(int ARRAY[], int SIZE);
int main()
{
int SIZE(0);
int* ARRAY;
cout << "Enter number of elements: ";
cin >> SIZE ;
ARRAY = new int[SIZE];
int x(0);
int numEle(0);
cout << "Enter list: " << endl;
while (numEle < SIZE)
{
ARRAY[numEle] = x;
numEle++;
cin >> x;
}
int POS(0), NEG(0);
count(ARRAY, SIZE, NEG, POS);
int* NEG_ARRAY;
NEG_ARRAY = new int[NEG];
int* POS_ARRAY;
POS_ARRAY = new int[POS];
split(ARRAY, SIZE, NEG_ARRAY, NEG, POS_ARRAY, POS);
cout << "Negative elements: " << endl;
cout << print_array(NEG_ARRAY, NEG) << endl;
cout << "Non-negative elements: " << endl;
cout << print_array(POS_ARRAY, POS) << endl;
delete [] ARRAY;
delete [] NEG_ARRAY;
delete [] POS_ARRAY;
return 0;
}
void count(int ARRAY[], int SIZE, int& NEG, int& POS)
{
for (int x=0; x < SIZE; x++)
{
if (ARRAY[x] >= 0)
{
POS = POS + 1;
}
if (ARRAY[x] < 0)
{
NEG = NEG + 1;
}
}
}
void split(int ARRAY[], int SIZE, int& NEG_ARRAY, int NEG, int& POS_ARRAY, int POS)
{
NEG = POS = 0;
for (int x = 0; x < SIZE; x++)
{
if (ARRAY[x] < 0)
{
NEG_ARRAY[NEG++] = ARRAY[x];
}
else
{
POS_ARRAY[POS++] = ARRAY[x];
}
}
}
void print_array(int ARRAY[], int SIZE)
{
for (int i = 0; i < SIZE; i++)
{
cout << ARRAY[i] << " ";
}
cout << endl;
}
該代碼應該在數組中讀取並顯示一個新的負數和一個新的正數組。 提前致謝!
您有以下錯誤:
void split(int ARRAY[], int SIZE, int&NEG_ARRAY, int NEG, int&POS_ARRAY, int POS);
改成 :
void split(int ARRAY[], int SIZE, int*NEG_ARRAY, int NEG, int*POS_ARRAY, int POS);
還有:
void split(int ARRAY[], int SIZE, int&NEG_ARRAY, int NEG, int&POS_ARRAY, int POS){..}
改成 :
void split(int ARRAY[], int SIZE, int*NEG_ARRAY, int NEG, int*POS_ARRAY, int POS){..}
和
cout<<print_array(NEG_ARRAY, NEG) <<endl
cout<<print_array(NEG_ARRAY, POS) <<endl;
至 :
print_array(NEG_ARRAY, NEG);
print_array(NEG_ARRAY, POS);
修復這些錯誤后,它可以編譯並運行良好。
您的代碼中存在大量錯誤。 最糟糕的是通過聲明和split
函數定義中的引用傳遞數組。 將兩者都更改為void split(int ARRAY[], int SIZE, int *NEG_ARRAY, int NEG, int *POS_ARRAY, int POS);
,大多數錯誤都將消失。
其余部分來自您在main
打印數組的兩行:
cout<<print_array(NEG_ARRAY, NEG) <<endl;
您不想打印該功能,您想使用該功能在其中打印(您正確執行)。 您需要將調用更改為:
print_array(NEG_ARRAY, NEG);
這將使您的代碼編譯。
Hovewer還有一個錯誤,這將使整個應用程序以不正確的方式工作。 在輸入值的位置,您需要在輸入數組之前從cin
獲取輸入。 像這樣:
while(numEle<SIZE) {
cin>>x;
ARRAY[numEle] = x ;
numEle++;
}
首先,使用std::vector
幾乎總是比使用動態分配的C數組更好。 你沒有獲得指針和方括號數組訪問的可怕混合,並且你不需要傳遞額外的大小變量。
其次,標准庫有一些很好的算法來幫助你做你想做的事情。 讓我們假設你將給定的數字寫入一個名為vec
的向量中。 然后,您可以使用std::partition
將小於零的所有元素移動到向量的前半部分,將所有大於或等於零的元素移動到后半部分,如下所示:
inline bool less_than_zero(int a)
{
return a < 0;
}
std::vector<int>::iterator midpoint = std::partition(vec.begin(),
vec.end(),
less_than_zero);
(還有其他指定謂詞的方法,但是這樣的簡單函數定義最容易用於演示目的。)
返回的迭代器指向向量中的第一個非負數項。 所以現在您可以輕松地將值復制到兩個新向量中:
std::vector<int> negative(vec.begin(), midpoint);
std::vector<int> positive(midpoint, vec.end());
就是這樣!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.