簡體   English   中英

當我在多個數組中有更多輸入數據時,如何在 C++ 中創建排序結構?

[英]How can I make a sorted structure in C++ when I have more input data in more than one array?

我需要按年制作一個排序列表/結構,但我不知道如何制作。 這是一些數據,例如每個數組的第一個數據:年:2010 月:1,溫度為-10.4C。 我剛開始學習 C++,但它抓住了我。 順便提一句。 對不起我的英語。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

const int LENGTH = 24;

struct list
{
    int year;
    int month;
    double temp;
    list *next;
};

int main()
{
    int year[LENGTH]={2010, 2010, 2011, 2011, 2011, 2017, 2017, 2016, 2016, 2016, 2015, 2015, 2013, 2013, 2012, 2011, 2015, 2016, 2017, 2011, 1013, 2014, 2015, 2016 };
    int month[LENGTH]={1 , 2 , 1 , 2 , 3 , 7, 8, 2, 6, 7, 3, 5, 10, 11, 12, 5, 6, 10, 10, 11, 12, 4, 11, 12 };
    double temp[LENGTH]={-10.4,-2.8, -5.1,-2.1, 3.5 , 30.9, 35.7, -7.3, 20.3, 34.2, 6.2, 15.4,10.13, 1.56,-12.7, 15.8, 16.2, 0.21, 9.9, 4.4, -3.3, 4.7, 0, 20.4 };

//i can only make this

list *pStart = NULL, *pNext;
list *p;

for (int index = 0; index<LENGTH; index++) {
     p->year = year[index];
     p->month = month[index];
     p->temp = temp[index];
     p = p->next;
}

//and here need to print it after sort
//Just an idei sort(years) and after it upload/fill the struct or do you know a better way?
}

開始學習一門新語言總是很辛苦的。 使用 C++,C 的遺產增加了學習曲線,因為有舊的 (C) 方式和更新的 (C++) 方式來做事。 是的,您可以編寫非常類似於 C 的 C++ 程序。 但是,您並沒有獲得 C++ 所提供的好處。

即:

  • 一個更全面的標准庫。
  • 更少需要像指針這樣的危險東西。
  • 一堆有用的開箱即用數據類型(集合,...)。

如果它不是作業的一部分(例如作業),則 C++ 程序員首先考慮的默認集合類型是std::vector<T> 除非您知道它始終是固定的且大小相同,並且您需要額外的性能。 然后你可能會使用std::array<T,N> 只有在特殊情況下,當您有充分的理由並且可能分析數據來支持決策時,您才會考慮使用基於列表的容器。 (標准::出隊,...)。

至於排序, algorithm header 文件中有std::sort()供您使用,它有很多重載。 其中一個重載允許您指定要在排序期間應用的比較 function。 鑒於您只想按年份對數據進行排序,而結構中有更多數據,這就是您想要的。

下面,請找到我想出的代碼 - 我希望 - 做你想要的。

#include <iostream>
#include <cstdint>
#include <vector>
#include <algorithm>

template <class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& value) {
  os << "vector(" << value.size() << "):" << std::endl << "{";
  if (value.size() > 0) {
    os << value[0];
  }
  for ( size_t i = 1; i < value.size(); ++i) {
    os << ", " << value[i];
  }
  os << "}";
  return os;
}

struct DataPoint {
  uint16_t year;
  uint8_t month;
  double temp;
  DataPoint( uint16_t y, uint8_t m, double t)
    : year{y}
    , month{m}
    , temp{t}
  {}
  DataPoint(const DataPoint& other)
    : year{other.year}
    , month{other.month}
    , temp{other.temp}
  {}
};

std::ostream& operator<<(std::ostream& os, const DataPoint& value) {
  os << "DataPoint: {"
     << value.year << ", "
     << (int)value.month << ", " // cast needed because iostream stupid.
     << value.temp << "}";
  return os;
}

using DataSet = std::vector<DataPoint>;

bool initDataSet(DataSet& target) {
  constexpr size_t LENGTH = 24;
  target.clear();
  target.reserve(LENGTH);
  uint16_t year[LENGTH]=
    {2010, 2010, 2011, 2011, 2011, 2017, 2017, 2016, 2016,2016,
     2015, 2015, 2013, 2013, 2012, 2011, 2015, 2016, 2017, 2011,
     1013, 2014, 2015, 2016 };
  uint8_t month[LENGTH]=
    {1 , 2 , 1 , 2 , 3 , 7, 8, 2, 6, 7,
     3, 5, 10, 11, 12, 5, 6, 10, 10, 11,
     12, 4, 11, 12 };
  double temp[LENGTH]=
    {-10.4,-2.8, -5.1,-2.1, 3.5 , 30.9, 35.7, -7.3, 20.3, 34.2,
     6.2, 15.4,10.13, 1.56,-12.7, 15.8, 16.2, 0.21, 9.9, 4.4,
     -3.3, 4.7, 0, 20.4 };
  for (size_t i=0; i < LENGTH; ++i) {
    target.push_back(DataPoint(year[i], month[i], temp[i]));
  }
  return true;
}

int main(int argc, const char* argv[]) {
  DataSet data;
  if (initDataSet(data)) {
    std::cout
      << "Before sorting"
      << std::endl
      << data << std::endl;

    std::sort(data.begin(),data.end(),
          [](const DataPoint& a, const DataPoint& b) -> bool {
        return a.year < b.year;
          });

    std::cout
      << "After sorting"
      << std::endl
      << data << std::endl;
  }
  return 0;
}

它還顯示了使ìostream output 成為您認為合適的任何數據類型的標准方法。

作為練習,您可以嘗試重新編寫此代碼以使用std::forward_list<T>而不是向量。 您不能使用std::sort() ,因為單鏈表只有一個前向迭代器,而不是一個隨機訪問迭代器。 但幸運的是,您會發現std::forward_list<T>有一個名為sort()的成員 function 。 因此,重寫將相當少。

暫無
暫無

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

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