排序后如何将二维数组重置为原始形式

How to reset two-dimensional array to its original form after sorting?

本文关键字:原始 二维数组 排序      更新时间:2023-10-16

我试图用气泡排序对其进行排序后,将2D数组重置为原始形式。我需要将其重置为排序之前。我该怎么做?如果您有一个疑问,为什么数组是全局的。这是一项学校任务,这就是我们的教授希望我们这样做的方式。这是我的程序:

 #include<iostream>
    using namespace std;
    const int NUM_COLS=4;
    const int NUM_ROWS=5;       
    int array[NUM_ROWS][NUM_COLS]={{5, 3, 2, 16},
                            {9, 8, 10, 17},
                            {4, 7, 11, 18},
                            {2, 5, 9, 12},
                        {7, 9, 4, 10}};

它与bubblesort

对数组进行排序
void bubbleSort(int row, int col){}

它是显示数组功能标头

void displayArray(){}

这是主要功能

int main(){

        cout<<"original array"<<endl;
        displayArray();
        bubbleSort(NUM_ROWS-1, NUM_COLS);
        cout<<"nbubble sort"<<endl;
        displayArray();
        reset();
        displayArray();
        return 0;
    }

现在,我需要将数组重置为原始数。我这样做了,但它行不通。

void reset(){
 int array[NUM_ROWS][NUM_COLS]={{5, 3, 2, 16},
                                {9, 8, 10, 17},
                                {4, 7, 11, 18},
                                {2, 5, 9, 12},
                                {7, 9, 4, 10}};
}

您的 reset正在声明一个新数组(对此无能为力)。您无法分配(=)C样式数组,因此您需要看起来不同的东西。如果可以使用std::array,则可以在reset中分配。

#include <array>
const int NUM_COLS=4;
const int NUM_ROWS=5;       
std::array<std::array<int, NUM_ROWS>, NUM_COLS> values = {
    {5, 3, 2, 16},
    {9, 8, 10, 17},
    {4, 7, 11, 18},
    {2, 5, 9, 12},
    {7, 9, 4, 10}};
// Other code probably remains unchanged
void reset() {
    values = {
        {5, 3, 2, 16},
        {9, 8, 10, 17},
        {4, 7, 11, 18},
        {2, 5, 9, 12},
        {7, 9, 4, 10}};
}

在此时,您会注意到自己的界限是错误的,应该是

const int NUM_COLS=5;
const int NUM_ROWS=4;       

或不同形状的阵列初始评估器。

void reset(){
  static int original[NUM_ROWS][NUM_COLS]={{5, 3, 2, 16},
                                {9, 8, 10, 17},
                                {4, 7, 11, 18},
                                {2, 5, 9, 12},
                                {7, 9, 4, 10}};
  for (int i = 0; i < NUM_ROWS; i++)
    memcpy(array[i], original[i], NUM_COLS * sizeof(int));
}

不是最漂亮的事情,但这应该起作用。既然这就是您的教授希望您这样做的方式,请继续前进。

正如我在评论中所说的那样,分配数组的最简单方法是将它们包裹在结构中。瞧,突然c 发展能力,甚至不知道它是从C和副本阵列继承的! 1 甚至嵌套了多维数组!

#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
const int NUM_COLS=4;
const int NUM_ROWS=5;       
// Define a struct (i.e., a class with all public members)
// which has just a single member, the array. Note that this is
// only a *type* declaration, no object is created yet.
struct arrT
{
  int array [NUM_ROWS][NUM_COLS];
};
// object creation.
arrT workArr;
void reset()
{
  // The initialization value is hidden inside the function.
  // static variables are initialized only once, for constant 
  // data at compile time.
  static const arrT oriArr 
  {
    { {5, 3, 2, 16},
      {9, 8, 10, 17},
      {4, 7, 11, 18},
      {2, 5, 9, 12},
      {7, 9, 4, 10}
    }
  };
  workArr = oriArr; // simple default assignment of structs
}
// The parameters are redundant.
void stdSort(int /*row*/, int /*col*/)
{ 
  // Sort the 2D array as a one-dimensional sequence
  // (which the elements are in memory).
  // The algorithm expects iterators to the first and
  // one-after-the-last elements in the sequence. Pointers
  // to the elements in an array are perfectly good iterators.
  std::sort(&workArr.array[0][0], &workArr.array[NUM_ROWS-1][NUM_COLS]);
}
void displayArray()
{
  // The top-level elements of a 2D array are the rows...
  for(auto &row: workArr.array)
  {
    // ... and the elements of the rows are ints. 
    // Note how the
    // dimensions are known from the class declaration.
    for(auto &el: row)
    {
      cout << setw(4) << el;
    }
    cout << "n";
  }
}
int main(){
  cout << "Work array before initialization:n";
  displayArray();
  reset(); // before, the values of the global array are 0.
  cout<<"nWork array after init:n";
  displayArray();
  stdSort(NUM_ROWS, NUM_COLS);
  cout<<"nWork array after std sort"<<endl;
  displayArray();
  reset();
  cout << "nWork array after resetn";
  displayArray();
  return 0;
}


1 数组是我在袖口上唯一知道的示例,在该袖口上生成的默认分配运算符的成员赋值可以分配一个没有独立分配运算符的类型(这是确切的原因我们跳过这个箍)。还有其他吗?