Reputation:
I have a function template that is supposed to take a vector and produce random numbers inside it. However, when I print entire vector, it's all zeros.
code:
const int smallSize = 20;
// declare small vector
vector <int> smallVector(smallSize);
genRand(smallVector, smallSize);
// make copy of small vector
vector<int> copySmallVector = smallVector;
// function template for generating random numbers
template<class T, class B>void genRand(T data, B size)
{
for (B i = 0; i < size; i++)
{
data[i] = (1 + rand() % size);
}
}
Upvotes: 1
Views: 48
Reputation: 213059
You are generating random numbers in a copy of your vector, and then throwing it away when the function returns.
Change:
template<class T, class B>void genRand(T data, B size)
to:
template<class T, class B>void genRand(T &data, B size)
^^^^^^^
Upvotes: 5