【问题标题】:Splitting array into separate positive and negative arrays c++将数组拆分为单独的正数组和负数组c ++
【发布时间】:2013-11-15 02:52:46
【问题描述】:

我需要编写一个函数,它接受一个给定的数组,然后将其拆分为两个单独的数组,一个数组的元素是主数组的正元素,另一个元素是主数组的负元素。我似乎无法弄清楚执行此操作的循环是什么样的。

我写了一个单独的函数来确定主数组中有多少正负值:

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)

我只是不知道如何将主数组中的每个正元素设置为新的 Positive-Only 数组中的元素,对于负数组也是如此。

感谢您的帮助!

在使用给出的答案并尽力使用其余代码后,我在尝试编译时遇到了大约一百万行错误。我如何删除三个动态分配的数组有问题吗?阻止编译的巨大错误是什么? 这是我的代码:

#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;
}

代码应该读入数组并显示新的负数和正数数组。提前致谢!

【问题讨论】:

  • 零怎么办?他们应该落入哪一边?是否有理由使用原始数组而不是像std::vector 这样的更高级别的构造?为什么输入数组是int ARRAY[],却是负数和正数int&amp;
  • 是否假定“正”和“负”数组预先存在,或者该函数必须为它们分配空间?
  • 我发布了我的整个代码。它不会编译并给我带来很多可笑的错误。

标签: c++ arrays


【解决方案1】:

你可能会得到一些 C 风格的答案

但是在这里我将如何使用 STL 算法,因为它被标记为 C++

使用std::partition

bool is_pos(int i) { return i > 0; }

auto p = std::partition(std::begin(ARRAY), 
         std::end(ARRAY), std::ptr_fun(is_pos));

std::copy(std::begin(ARRAY), p, std::begin(POS_ARRAY));

std::copy(p,  std::end(ARRAY), std::begin(NEG_ARRAY));

您也应该使用std::vector 进行此类操作

演示Here

【讨论】:

  • 非常灵巧,但对于像 OP 这样的初学者来说相当先进。
【解决方案2】:

此代码会将负数和正数划分为单独的数组,

void split(int ARRAY[], int SIZE, int NEG_ARRAY[], int&NEG, int POS_ARRAY[], int&POS)
{
    NEG=POS=0;
    for (int i(0); i<SIZE; i++)
    {
        if (ARRAY[i]<0) NEG_ARRAY[NEG++]=ARRAY[i];
        else POS_ARRAY[POS++]=ARRAY[i];
    }
}

【讨论】:

    【解决方案3】:

    修改count()函数很容易:

    void split(int ARRAY[], int SIZE, int NEG [], int POS [])
    {
      int ncount = 0, pcount = 0;
      for (int x=0; x<SIZE; x++)
        {
          if(ARRAY[x]>=0)
          {
              POS[pcount++] = ARRAY[x];
          }
          if(ARRAY[x]<0)
          {
              NEG[ncount++] = ARRAY[x];
          }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      相关资源
      最近更新 更多