11.7. A single-pass solution

Although this code works, it is not as efficient as it could be. Every time it calls how_many, it traverses the entire vector. In this example we have to traverse the vector ten times!

It would be better to make a single pass through the vector. For each value in the vector we could find the corresponding counter and increment it. In other words, we can use the value from the vector as an index into the histogram. Here’s what that looks like:

vector<int> histogram (upper_bound, 0);

for (const int& value: numbers) {
  ++histogram[value];
}

The first line initializes the elements of the histogram to zeroes. That way, when we use the increment operator (++) inside the loop, we know we are starting from zero. Not initializing our data to 0 is another form of undefined behavior and a common error.

The loop has the same assumption as before: the index position of the histogram vector is the value in the numbers vector.

Try this!

Encapsulate this code in a function called histogram that takes a vector and the range of values in the vector (in this case 0 through 9), and that returns a histogram of the values in the vector.

Construct a function called histogram that takes a vector and the range of values in the vector, and that returns a histogram of values in the vector.

You have attempted of activities on this page