简体   繁体   English

将数组拆分为单独的正负数组C ++

[英]Splitting array into separate positive and negative arrays c++

I need to write a function that takes a given array and then splits it into two separate arrays with one array's elements being the positive elements of the main array and the other's elements being the negative elements of the main array. 我需要编写一个函数,该函数接受给定的数组,然后将其拆分为两个单独的数组,其中一个数组的元素是主数组的正元素,另一个数组的元素是主数组的负元素。 I can't seem to figure out what the loop to do this would look like. 我似乎无法弄清楚执行此操作的循环是什么样的。

I have written a separate function to determine how many positive and negative values are in the main array: 我编写了一个单独的函数来确定主数组中有多少个正值和负值:

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

This counts the positives and negatives and the number of each would be the size of the respective positive and negative arrays after the split. 这将计算正负数,每个数将是拆分后各个正负数组的大小。

I have defined the function as such: 我已经定义了这样的功能:

 void split(int ARRAY[], int SIZE, int&NEG_ARRAY, int NEG, int&POS_ARRAY, int POS)

I just don't know how to set each of the positive elements in the main array as the elements in the new Positive-Only array and likewise for the negative array. 我只是不知道如何将主数组中的每个正数元素设置为新的“仅正数”数组中的元素,同样将其设置为负数数组。

Thanks for your help! 谢谢你的帮助!

After using the answers given and doing my best with the rest of the code, I got about a million lines of errors when trying to compile it. 使用给出的答案并尽力处理其余代码后,尝试编译时出现了大约一百万行错误。 Is there a problem with how I am deleting the three dynamically allocated arrays? 我如何删除三个动态分配的数组有问题吗? What huge error is preventing compiling? 哪些巨大的错误正在阻止编译? Here is my code: 这是我的代码:

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

The code is supposed to read in the array and display the new negative and positive arrays. 该代码应该读入数组并显示新的负数组和正数组。 Thanks in advance! 提前致谢!

You may get some C style answers 您可能会得到一些C风格的答案

But here how I'd do using STL algorithms, as this is tagged for C++ 但是这里我将如何使用STL算法,因为这是为C++标记的

Use std::partition 使用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));

Also you should use std::vector for such operations 另外,您应该使用std::vector进行此类操作

Demo Here 在这里演示

Its easy to modify your count() function: 它很容易修改您的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];
      }
    }
}

This code will divide negative & positive numbers into separate arrays, 这段代码会将负数和正数分成单独的数组,

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

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

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