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

问题描述 投票:-1回答:3

在尝试使用冒泡排序后,我尝试将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}};
}
c++ arrays reset dimensional
3个回答
2
投票

你的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;       

或者不同形状的阵列初始化器。


0
投票
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));
}

不是最漂亮的东西,但这应该有效。既然你的教授希望你这样做,那就去吧_ \ _(ツ)_ /¯


0
投票

正如我在评论中所说,分配数组的最简单方法是将它们包装在一个结构中。 Voilà,突然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 reset\n";
  displayArray();
  return 0;
}


1 Arrays are the only example I know of off the cuff where the memberwise assignment of the generated default assignment operator can assign a type which does not have a standalone assignment operator (which is the exact reason we jump through this hoop). Are there others?
© www.soinside.com 2019 - 2024. All rights reserved.