创建一个大对象C++数组

创建一个大对象C++数组,c++,arrays,object,C++,Arrays,Object,我需要创建一个大的直方图数组,其中每个直方图略有不同。我是C++的新手,我的第一个想法是使用for循环,它将创建直方图并将它添加到循环中的数组中,但是我遇到了我预期的变量名问题。在循环中添加直方图时,如何使每个直方图的变量名不同 很抱歉,如果措辞不好。听起来您想要的是一个直方图类,其中每个实例都有一点不同 class Histogram { unsigned m_count; std::string m_label; public: Histogram(std::stri

我需要创建一个大的直方图数组,其中每个直方图略有不同。我是C++的新手,我的第一个想法是使用for循环,它将创建直方图并将它添加到循环中的数组中,但是我遇到了我预期的变量名问题。在循环中添加直方图时,如何使每个直方图的变量名不同


很抱歉,如果措辞不好。

听起来您想要的是一个直方图类,其中每个实例都有一点不同

class Histogram {
    unsigned m_count;
    std::string m_label;
public:
    Histogram(std::string label) : m_count(0), m_label(label) {}
    std::string & label () { return m_label; }
    std::string label () const { return m_label; }
    unsigned & count () { return m_count; }
    unsigned count () const { return m_count; }
};
在地图中管理这些可能比在向量中更容易,除非您可以将输入分类为一个数字,但每个直方图都需要一个唯一的标签

std::map<std::string, std::unique_ptr<Histogram> > histograms;

while (more_input()) {
    input = get_input();
    std::string classification = classify_input(input);
    if (histograms[classification] == 0)
        histograms[classification]
            = std::unique_ptr<Histogram>(new Histogram(classification));
    histograms[classification]->count() += 1;
}

听起来您想要的是一个直方图类,其中每个实例都略有不同

class Histogram {
    unsigned m_count;
    std::string m_label;
public:
    Histogram(std::string label) : m_count(0), m_label(label) {}
    std::string & label () { return m_label; }
    std::string label () const { return m_label; }
    unsigned & count () { return m_count; }
    unsigned count () const { return m_count; }
};
在地图中管理这些可能比在向量中更容易,除非您可以将输入分类为一个数字,但每个直方图都需要一个唯一的标签

std::map<std::string, std::unique_ptr<Histogram> > histograms;

while (more_input()) {
    input = get_input();
    std::string classification = classify_input(input);
    if (histograms[classification] == 0)
        histograms[classification]
            = std::unique_ptr<Histogram>(new Histogram(classification));
    histograms[classification]->count() += 1;
}

C++具有内置数组;只需要一个名字。更好的是,使用std::vector。如果您向我们展示代码的相关部分,我们可以更好地帮助您;欢迎来到SO。试着去做,即使它很笨重或者不起作用,否则很难理解你的观点。此外,您还可以使用直方图myHistograms[1000]或std::vector myHistograms。但是实际的实现取决于你的直方图,尤其是它的构造函数和赋值方法。你可能不得不使用动态内存,因为大多数编译器对静态内存分配的限制较小。C++有内置数组;只需要一个名字。更好的是,使用std::vector。如果您向我们展示代码的相关部分,我们可以更好地帮助您;欢迎来到SO。试着去做,即使它很笨重或者不起作用,否则很难理解你的观点。此外,您还可以使用直方图myHistograms[1000]或std::vector myHistograms。但是实际的实现取决于你的直方图,特别是它的构造函数和赋值方法。你可能必须使用动态内存,因为大多数编译器对静态内存分配的限制较小。事实证明,我是个白痴,而且我一直在做错事。非常感谢你们的帮助。事实证明我是个白痴,我一直在做错事。非常感谢你们的帮助。