Reputation: 162
I need to create a large array(1000) of histograms where each histogram is slightly different. I am new to C++ and my first thought on how to do this was using a for loop which will create the histogram and add it to the array in the loop, but ive run into the issue of variable names (which i expected). How can i make the variable name of each histogram different while adding them in the loop?
sorry if that was poorly worded.
Upvotes: 1
Views: 488
Reputation: 70422
It sounds like what you want is a histogram class where each instance is a little different.
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; }
};
It might be easier to manage these within a map
rather than a vector
(unless you can actually classify the input into a number), but each histogram will need a unique label.
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;
}
Upvotes: 4