mardi 3 janvier 2017

Is it better to use pass by reference to fill a container or use the return value to fill it instead?

Sorry for the confusing title, and sorry if this is a duplicate (I tried searching for an answer online), hopefully this example clears it up:

Basically, would it be better to do this:

void fill(const vector<int> & v);

int main() {
  vector<int> v;
  fill(v);
  return 0;
}

or this:

vector<int> fill();

int main() {
  vector<int> v = fill();
  return 0;
}

I've been reading about how in C++11 the compiler will move the return results of functions rather than copying them. Is one of these better than the other? Or is it simply preference?

Aucun commentaire:

Enregistrer un commentaire