【问题标题】:How to make an object and put it into an array using a loop, so if I want to add data all I have to do is make a new string如何创建一个对象并使用循环将其放入数组中,所以如果我想添加数据,我所要做的就是创建一个新字符串
【发布时间】:2022-01-10 03:34:44
【问题描述】:

如何制作一个对象并将其放入数组中?我想用参数化的构造函数创建一个DailyStats 的数组。我想使用循环添加到对象数组中。

#ifndef DAILYSTATS_H
#define DAILYSTATS_H

#include <iostream>
#include <vector>
using namespace std;

class DailyStats
{
    public:
        DailyStats();
        DailyStats(string content);
        void parse(const string& line);
        double mean();
        string getDate();
        void setDate(string newDate); 

    private:
        string date;
        double temperatureValues[];
};

#endif
#include <iostream>
#include "DailyStats.hpp"
#include <vector>
#include <cmath>
using namespace std;
    
DailyStats::DailyStats()
{
    date = "";
    count = 0;
    temperatureValues[0];
}

DailyStats::DailyStats(string content)
{
    parse(content);
    temperatureValues[24];
    count = 0;
}

void DailyStats::parse(const string& line)
{
    string random = "";
    string random1 = "";
    char del = ' ';
    int count2 = 0;

    for(int i = 0; i <= (int)line.size(); i++)
    {
        if(count < 10)
        {
            random1 += line[i];
            setDate(random1);
            count++;
        }

        if(line[i] != del && count == 10)
        {
            random += line[i];
        }
        else if (line[i] == del && count == 10)
        {
            temperatureValues[count2] = stod(random);
            random = "";
            count2++;
        }
    }
}

double DailyStats::mean()
{
    count = 1;
    double num = 0;

    while(count <= 24)
    {
        num += temperatureValues[count];

        if(count == 24)
        {
            num = num/24;
        }

        count++;
    }

    return ceil(num * 100.0) / 100.0;
}

string DailyStats::getDate()
{
    return date;
}

void DailyStats::setDate(string newDate)
{
    date = newDate;
}
#include <iostream>
#include <string>
#include "DailyStats.hpp"
using namespace std;

int main()
{
    string str0 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str1 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str2 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str3 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";

    DailyStats info[4];

    info[1] = { {str0} };

    cout << info[1].mean() << endl;


    return 0;
}

正如您在我的代码中看到的那样,我有不同的字符串,我想将它们放入我的对象中,以用作我的参数化构造函数的变量。所以,当我完成这些后,我可以调用我的mean() 函数,以便它可以获取不同数据的不同方式。

我在没有数组的情况下尝试过,一切正常,但我不知道如何让数组工作。

【问题讨论】:

  • 这段代码中没有数组。请显示您尝试过的不适合您的代码。但是,不可能声明具有不同构造函数值的对象的 fixed 数组,因此您必须为该数组分配一块原始内存,然后使用placement-new 构造其中的每个对象根据需要记忆。否则,只需使用std::vector,然后您可以将构造的对象推入其中,并传入您想要的任何构造函数值。
  • @RemyLebeau 我已经更新了我的代码以显示我尝试过的内容
  • @RemyLebeau -- struct S { S(const std::string&amp;) {} }; S s[] = { str0, str1, str2, str3 }; 用定义的值构造一个数组。
  • 请阅读stackoverflow.com/help/minimal-reproducible-example。除非您有理由期望 DailyStats 类的内容与导致问题相关,否则您可以向我们展示该类的虚拟版本的示例。如果您“在没有数组的情况下尝试了它并且一切正常”,那么您应该向我们展示一个没有数组的代表性代码版本,然后分别展示使用数组的尝试并解释到底出了什么问题我>。 “我不知道如何让它工作”没有帮助; 什么不工作
  • @NixJoe 关于错误,double temperatureValues[]; 不是类成员数组的有效声明。这样的数组不能解绑。如果您在编译时不知道数组大小而只在运行时不知道数组大小,请改用std::vector

标签: c++ arrays


【解决方案1】:

我看到的主要问题是您的 temperatureValues[] 数组在编译时未绑定。这是行不通的。由于您直到运行时才知道数组的大小,因此请改用std::vector(实际上,您的代码中已经有#include &lt;vector&gt;),例如:

#ifndef DAILYSTATS_H
#define DAILYSTATS_H

#include <vector>
#include <string>

class DailyStats
{
    public:
        DailyStats() = default;
        DailyStats(const std::string& content);
        void parse(const std::string& line);
        double mean() const;
        std::string getDate() const;
        void setDate(const std::string& newDate); 

    private:
        std::string date;
        std::vector<double> temperatureValues;
};

#endif
#include "DailyStats.hpp"
#include <cmath>
#include <sstream>
    
DailyStats::DailyStats(const string& content)
{
    parse(content);
}

void DailyStats::parse(const string& line)
{
    std::istringstream iss(line);
    double value;

    iss >> date;

    temperatureValues.clear();
    while (iss >> value)
        temperatureValues.push_back(value);
}

double DailyStats::mean() const
{
    double num = 0.0;

    if (!temperatureValues.empty())
    {
        for(size_t count = 0; count < temperatureValues.size(); ++count)
        {
            num += temperatureValues[count];
        }
        num = std::ceil((num / temperatureValues.size()) * 100.0) / 100.0;
    }

    return num;
}

std::string DailyStats::getDate() const
{
    return date;
}

void DailyStats::setDate(const std::string& newDate)
{
    date = newDate;
}
#include <iostream>
#include <string>
#include "DailyStats.hpp"
using namespace std;

int main()
{
    string str0 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str1 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str2 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
    string str3 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";

    DailyStats info[4];

    info[0].parse(str0);
    info[1].parse(str1);
    info[2].parse(str2);
    info[3].parse(str3);

    cout << info[0].mean() << endl;
    cout << info[1].mean() << endl;
    cout << info[2].mean() << endl;
    cout << info[3].mean() << endl;

    return 0;
}

【讨论】:

  • 现在对我来说更有意义了,感谢您的帮助,我真的很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 2013-12-07
  • 2019-09-24
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
相关资源
最近更新 更多