簡體   English   中英

在C ++中排序我的二維數組

[英]Sorting my 2d array in c++

我的作業程序必須將到達時間和突發時間的隨機數寫入文件中。 然后,在寫入之后,它將讀取文件並對內容進行排序。

我認為設置2D數組對我來說是最簡單的方法。 但是我不確定如何實現我的排序,以便如果到達時間交換了位置,那么該到達的突發時間就會繼續。

我覺得我的措辭很差,但是一個基本的例子是:

array[3][10] > array[2][23]

因此,由於第二個數組具有較早的到達時間,因此我需要它的到達2和其突發23都必須在array[3][10]之前移動,但是我需要這樣做並比較100個輸入。

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>

const int max = 100;

using namespace std;

int main()
{
    multimap<int [][]> myMap;

    int randomBurst[max];
    int arrivalTime[max];
    int line[max][2];
    int first = 0;

    for (int i = 0; i < 100; i++)
    {
        if (i < 100)
        {
            ofstream write("Schedule.txt", ios::app);
            randomBurst[i] = rand() % 1000;
            arrivalTime[i] = rand() % 1000;
            write << arrivalTime[i] << " " << randomBurst[i] << endl;
        }
    }
    ifstream read("Schedule.txt");
    for (int i = 0; i <= max; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            read >> line[i][j];

            cout << line[i][j] << " " ;
        }
        cout << endl;
    }

    cout << endl;
    cout << endl;
    for (int i = 0; i <= max; i++)
    {
    for (int j = 0; j < 2; j++)
    {

        myMap.insert(pair<int[][]>(line[i][j]);

    }
    cout << endl;

    }
    system("pause");
    return 0;
}

我的代碼在讀取寫入的文件內容后正確設置了我的數組,但是我有點迷失了應該執行的排序工作。

很好地提出這個建議,主要是留下那條評論以便能夠在我的筆記本電腦上更快地找到這個問題。

就像我在評論中說的那樣,如果您想按鍵值2D“數組”進行預排序,則最快的方式是使用地圖容器。如果您確實需要對內部點進行排序,並且您將使用其中的多個條目,比方說,條目2,30 2,12 ...您可以構建矢量或數組的映射,也可以使用Multimap。 不太確定這個數據結構,因為到目前為止我還沒有真正使用它的理由。 在這里引用http://www.cplusplus.com/reference/map/multimap/

上面的內容將為您完成排序,而我建議使用向量的原因是向量內缺乏順序,並且不確定是否“爆發”? 也要訂購。

編輯:忘了提一下,一張地圖將不保存任何給定值的多個鍵,因此,如果您再次在上面輸入多個點,那么您會。 如果像以前那樣實現,請覆蓋。

編輯:所以這或多或少是我認為的解決方法,但是您以非常間接的方式來解決此問題,很難誠實地遵循。

    #include <map>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
using namespace std;

const int MAX = 100;
int main()
{
  multimap<int,int> myMap;

  int randomBurst[100];
  int arrivalTime[100];
  int line[100][2];
  int first = 0;

  for (int i = 0; i < 100; i++)
    {
      if (i < 100)
        {
          ofstream write("Schedule.txt", ios::app);
          randomBurst[i] = rand() % 1000;
          arrivalTime[i] = rand() % 1000;
          write << arrivalTime[i] << " " << randomBurst[i] << endl;
        }
    }
  ifstream read("Schedule.txt");
  for (int i = 0; i <= 100; i++)
    {
      for (int j = 0; j < 2; j++)
        {
          read >> line[i][j];

          cout << line[i][j] << " " ;
        }
      cout << endl;
    }
  // cout << endl;                                                              
  // cout << endl;                                                              
  for (int i = 0; i < 100; i++)
    {
      for (int j = 0; j < 2; j++)
        {
          //Attain the value in the index, and the held value within it.        
          myMap.insert(pair<int, int> (line[i][j], line[i][j]));
        }
      cout << endl;

    }
  //    system("pause");                                                        
  return 0;

這可以修復插入點,只是因為您給它提供了一個數組,並不意味着程序會將其作為一對,因為第一個索引本身就是指向另一個數組的點。 等等。 我建議改用一個地圖對象作為起點,因為多圖會使事情有些煩人,如果您熟悉矢量容器,則可以在地圖中使用它來記錄多個值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM