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.
- Your code runs without a problem because counters are automatically initialized to zero.
- Incorrect! Variables are not automatically initialized.
- Your code might run, but it probably won't produce the output you desire.
- Correct! C++ might assign unused memory to the uninitialized variable, which will allow the code to run, but counts may be off.
- You might get an error for using an uninitialized variable.
- Correct! Depening on your compiler, you might be lucky enough to get an error message.
- Your program will crash.
- Incorrect! You might get a compile error, but your program will not crash.
Q-1: What happens if you don’t initialize a counter?
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.