I am trying to return a vector from a function. But if I return the whole vector, then the copying process would take extra time, so I am trying to return a reference to the vector only. How do I do that in C++ ? here's my current function,
vector<ii>& primeFactors(int n)
{
int i;
vector<ii> factors;
for(i=0; i<=n && n!=1 ; ++i)
{
int cnt = 0;
while((n%primes[i]) == 0)
{
++cnt;
n/=primes[i];
}
if(cnt!=0)
factors.push_back({primes[i],cnt});
}
if(i == lim)
factors.push_back({n,1});
return factors;
}
and heres my calling in main()
vector<ii> factors = primes.primeFactors(20);