Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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++;当我在多个数组中有多个输入数据时?_C++ - Fatal编程技术网

C++ 如何在C++;当我在多个数组中有多个输入数据时?

C++ 如何在C++;当我在多个数组中有多个输入数据时?,c++,C++,我需要做一个按年份排序的列表/结构,但我不知道怎么做。 以下是一些数据,例如来自每个阵列的第一个数据: 年份:2010月份:1,温度为-10.4摄氏度。 我刚开始学习C++,但它吸引了我。 顺便说一句,对不起我的英语 #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; const int LENGTH = 24; struct list {

我需要做一个按年份排序的列表/结构,但我不知道怎么做。 以下是一些数据,例如来自每个阵列的第一个数据: 年份:2010月份:1,温度为-10.4摄氏度。 我刚开始学习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?
}
#包括
#包括
#包括
使用名称空间std;
常数int长度=24;
结构列表
{
国际年;
整月;
双温;
列表*下一步;
};
int main()
{
国际年[长度]={2010、2010、2011、2011、2017、2017、2016、2016、2016、2015、2015、2013、2012、2011、2015、2016、2017、2011、1013、2014、2015、2016};
整月[长度]={1,2,1,2,3,7,8,2,6,7,3,5,10,11,12,5,6,10,10,11,12,4,11,12};
双温[长度]={-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,4.4,-3.3,4.7,0,20.4};
//我只能做到这一点
列表*pStart=NULL,*pNext;
列表*p;
对于(int指数=0;indexyear=年份[指数];
p->月=月[指数];
p->temp=temp[索引];
p=p->next;
}
//这里需要在排序后打印出来
//只是一个想法排序(年),然后上传/填充结构,或者你知道更好的方法吗?
}

开始学习一种新语言总是有一定的作用。用C++,C的继承会增加学习曲线,因为有旧的(C)方式和一种更新的(C++)方式来做事情。是的,你可以编写C++程序,它是相当类似于C的。但是你不能从C++中得到好处。

即:

  • 更全面的标准库
  • 不需要像指针这样危险的东西
  • 一组有用的现成数据类型(集合,…)
如果它不是作业的一部分(例如家庭作业)C++程序员首先考虑的默认集合类型是<代码> STD::vector < /Cord>。除非您知道它总是固定的和相同的大小,而且需要额外的性能。然后,您可能会使用<代码> STD::数组< /C> >。只有在特殊情况下,当您有充分的理由并且可能分析数据来支持决策时,W。您可以考虑基于列表的容器。(STD::DEQue..…) 至于排序,有
std::sort()
算法
头文件中,由您处理,其中包含一系列重载。其中一个重载允许您指定排序期间应用的比较函数。如果您希望仅按年份对数据进行排序,而结构中有更多数据,这就是您想要的

#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;
}

下面,请找到我提出的代码,我希望它能满足您的需求

#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;
}

#包括
#包括
#包括
#包括
模板

STD:OrStand:如果你有24年的时间,每个12个月,你应该有<代码> 24×12=288 < /Cord>温度,不是吗?你必须创建你自己的链表吗?我猜你必须这样做。但是如果不是,一个STD容器将是行进的方式。在C++中使用<代码> NulLPTR < /代码>代替C<代码> null < /C> >,但是如果你可以,也尝试避免指针。。在用值初始化它之前,你不能触摸
p
。现在你只是在取消对未初始化指针的引用。也许你需要
新的
?你是否需要在这里重新发明轮子,因为教授或正在使用像
std::vector
这样明智和普通的选项?一个改进是初始化ve带有
初始值设定项的ctor\u list
。另一件事,您的排序还需要检查月份和年份是否相等。@问题的第一句以“我需要按年份制作一个排序的列表/结构”。至于initializer list,我将不得不重新格式化数据,而不是简单地从问题中复制和粘贴数据。