Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 合并两个数组元素还是将一个数组元素分配给另一个数组元素?_C++_Arrays - Fatal编程技术网

C++ 合并两个数组元素还是将一个数组元素分配给另一个数组元素?

C++ 合并两个数组元素还是将一个数组元素分配给另一个数组元素?,c++,arrays,C++,Arrays,我想将数组ab[I]的元素分配到posi[I](绝对值+正值) 然后,新数组应该是[1,2,3,4,5,9,8,7,6]或[1,2,3,4,5,6,7,8,9] 以下是我迄今为止所做的工作: #include <iostream> using namespace std; const int sentinel=0; const int arraysize=20; int main() { int numbers[arraysize]; int neg[arraysiz

我想将数组ab[I]的元素分配到posi[I](绝对值+正值)

然后,新数组应该是
[1,2,3,4,5,9,8,7,6]
[1,2,3,4,5,6,7,8,9]

以下是我迄今为止所做的工作:

#include <iostream>
using namespace std;
const int sentinel=0;
const int arraysize=20;
int main()
{
    int numbers[arraysize];
    int neg[arraysize];
    int posi[arraysize];
    int ab[arraysize];
    int count=0;
    int negcount=0;
    int posicount=0;
    int absolutecount=0;
    int absolute;
    int num;

    cout << "Enter ints -0 to quit: ";
    cin >> num;
    while (num != sentinel && count < arraysize)
    {

        numbers[count] = num;
        cin >> num;
     }

    //   Negative
    for (int i=0; i < count; i++)
    if ( numbers[i] < 0)
    {
        neg[negcount]=numbers[i];
        negcount++;
    }
    cout << "Absolute value: ";
    for (int i= negcount-1; i >= 0;i--)
    {
        absolute = neg[i]*-1;
        ab[i] =absolute;
        cout << ab[i] << ' ';
    }
    cout << endl;
    //   Negative

    for (int i=0; i < count; i++)
    if ( numbers[i] > 0)
    {
        posi[posicount] = numbers[i];
        posicount++;
    }
    cout << "Positive Number: ";
    for ( int i=0; i<posicount; i++)
        cout << posi[i] << ' ';
    cout << endl;
    return 0;
} 
#包括
使用名称空间std;
const int sentinel=0;
常数int arraysize=20;
int main()
{
整数[排列];
int neg[阵列化];
int posi[arraysize];
int ab[阵列化];
整数计数=0;
int negcount=0;
int posicount=0;
int绝对计数=0;
绝对整数;
int-num;
cout>num;
while(num!=sentinel&&count>num;
}
//否定的
for(int i=0;icout我认为没有必要使用
neg
pos
ab
数组来解决以您描述的形式重新排列数据的问题

因为这是C++,让我们使用一些STL算法函数:

#include <iostream>
#include <algorithm>

int main()
{
    int numbers[] = {1,2,3,4,5,-6,-7,-8,-9};

    // partition negatives from positive numbers
    auto iter = std::stable_partition(std::begin(numbers), 
                                      std::end(numbers), [](int n)
                                     { return n >= 0; });

    // reverse the negative numbers
    std::reverse(iter, std::end(numbers));

    // make the negative numbers positive
    std::for_each(iter, std::end(numbers), [](int& n){ n *= -1; });

    // output results
    for (auto& i : numbers)
      std::cout << i << " ";
}
#包括
#包括


请注意,不需要使用辅助数组来解决此问题。
numbers
数组只会重新排列和更改。

numbers[count]=num;
您不增加count:
numbers[count++]=num;
哪个是正确答案?是
[1,2,3,4,5,6,7,8,9]
还是
[1,2,3,4,5,9,8,7,6]
?只要是,就无所谓了synced@kamalilama然后,如果你真的想要C++答案,它需要3行代码,而不是循环的一组<>代码>。
#include <iostream>
#include <algorithm>

int main()
{
    int numbers[] = {1,2,3,4,5,-6,-7,-8,-9};

    // partition negatives from positive numbers
    auto iter = std::stable_partition(std::begin(numbers), 
                                      std::end(numbers), [](int n)
                                     { return n >= 0; });

    // reverse the negative numbers
    std::reverse(iter, std::end(numbers));

    // make the negative numbers positive
    std::for_each(iter, std::end(numbers), [](int& n){ n *= -1; });

    // output results
    for (auto& i : numbers)
      std::cout << i << " ";
}