Need help with First Three Multiples

Hi guys, I am currently on C++ module First Three Multiples Tut.

I have looked at ranged based for loops a bit to understand the for loop at the bottom, but I am stuck on why my code outputs so many 0’s. Could anyone explain to me why, and what I should do instead?

My aim for this tut is to print out 3 values of multiples of 8. 8 (81). 16 (82). 24 (8*3).

#include <iostream>
#include <vector>

// Define first_three_multiples() here:

std::vector <int> first_three_multiples(int num)
{
	std::vector<int> ftm(num);
	for (int i = 1; i <= 3; i++)
	{
		ftm.push_back(num * i);
	}
	return ftm;
}

int main() {

for (int element : first_three_multiples(8)) {
		std::cout << element << "\n";
	}
	return 0;
}

Cheers.

Han

Hello, welcome to the forum :slight_smile:

This is the crucial line:

std::vector<int> ftm(num);

take a look at the reference -> http://www.cplusplus.com/reference/vector/vector/vector/, this is the part that relates to this situation:

explicit vector (size_type n, const allocator_type& alloc = allocator_type());
vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type());
Constructs a container with n elements. Each element is a copy of val (if provided).

This means that in this line of code you have created a vector of type int with num (in case of this snippet num = 8) elements. Vector initialises all the elements and the default value for int is 0. That is why in the output you get eight zeros :slight_smile:

To fix this problem you can simply omit the size_type parameter:

std::vector<int> ftm;

Small digression that can be ignored.

But this should lead us to a question - why there is a way to specify the number of elements at the creation time of the vector - its length is extendable, right? Right. But when you initialise vector without specifying the size then with every push_back there might be a need to allocate memory (and this takes time). And sometimes this is what we want - sometimes we simply do not know how many elements will be in the vector.

But in case of this function, we are sure that we will push_back only 3 values. But we do not want to start with three zeros, we don’t know the values, we have to calculate them. In this scenario we can use reserve method to allocate memory for three int values:

std::vector<int> ftm;
ftm.reserve(3);
1 Like

Thanks for your response.

Are there any advantages to reserving the capacity verses to initializing the vector with int 0 as elements by default?

You’re very welcome :slight_smile:

If there is a high probability that many of the values will remain zeros then it might be better to initialize with zeros (or with any other value passed as a second parameter).

But when you don’t have any prior knowledge of what kind of values you will place inside the vector then it’s better to just reserve space for them.

It is pretty intuitive - the fewer operations a computer needs to execute, the faster the code will be.