If you want to be reasonably sure that your function doesn't copy the vector, then:
void func(const vector<obj> &input, vector<obj> &result);
If you are OK with relying on compiler "return value optimisation" (RTVO) (most good compilers do this):
vector<obj> func(const vector<obj> &input);
Return value optimisation is essentially where the compiler knows that we're returning a copy of a vector, so it removes the "extra copy" that would normally be needed in returning the content of, in this case, vector<obj>
from func
. A C++11 compliant compiler should use the "move constructor" if it is not able to do RTVO, which is also small cost in the whole scheme of things.
If you are happy to dynamically allocate the vector itself, you can return a pointer to it, but this is a poor solution:
vector<obj>* func(const vector<obj> &input);
This solution relies on allocating vector<obj>
, which means that something somewhere has to delete
the result of the call to func
. Maintenance of such code, if func
is called many times, gets rather nasty (especially if func
gets called lots of times, which may well be the case in a genetics analysis situation).
It is usually hard to achieve a return of a reference, so you are probably doing something wrong if you do - DO NOT DO THIS (unless you know what you are doing and why):
vector<obj>& func(const vector<obj> &input);
Of course, it REALLY depends on what you are doing with the vectors, and there may be other solutions that are even better, if you give a more concrete example of what you are doing inside the function.
By the way:
obj *func(obj *input)
is a maintenance nightmare, because if obj*
is allocated in func
, it is now for the caller to maintain this structure.
Finally, ALWAYS when you deal with performance, it's important to benchmark with YOUR compiler, on YOUR machine(s). Different compilers and different systems behave differently - guessing based on looking at the source-code is a very poor solution for understanding what code will be generated and how fast it is.