So I have a project that makes the user input an option op on which depends the type of the vector it will be used, that is vector<int>
vector<float>
and vector<double>
.
To do this, I have created 3 functions that will be called depending on op, and it works like this:
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> intv;
typedef vector<float> flov;
typedef vector<double> douv;
intv vectori(int opt, int t);
flov vectorf(int opt, int t);
douv vectord(int opt, int t);
int main(){
int opt;
size_t t;
cout << "Size: \n";
cin >> t;
cout << "Option? \n";
cin >> op;
flov vf;
douv vd;
intv vi;
switch (op){
case 00:
vi.reserve(t);
vi=vectori(0,t);
break;
case 01:
vf.reserve(t);
vf=vectorf(0,t);
break;
case 02:
vd.reserve(t);
vd=vectord(0,t);
break;
}
}
This would be the example vectori function:
intv vectori(int op, int t)
{
intv v;
v.reserve(t);
// Do stuff
return v;
}
And the example vectorf function is analogous (So is vectord):
flov vectorf(int op, int t)
{
flov v;
v.reserve(t);
// Do stuff
return v;
}
So the questions are:
- What is the memory cost of the statement vector vf , Considering I am not reserving memory for it if it is not needed.
- What happens with the variable v once the function vectorx ends? Does returning v and calling it vi create a copy of v in memory?
- Is this overall memory efficient?