-1

If a function takes an argument vector<int>&, what exactly does that mean? Is that the same as saying the value of the pointer that is contained in the vector? I completely understand vector<int&>; just something about the & being on the outside that trips me up.

UnworthyToast
  • 825
  • 5
  • 11
  • 21
  • Would help: [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), [What are the differences between pointer variable and reference variable in C++?](http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c?rq=1) – Ivan Aksamentov - Drop May 02 '14 at 04:18
  • Thats interesting. I am the opposite as you cuz I understand `vector&` but dont understand `vector` – SwiftMango May 02 '14 at 04:35

4 Answers4

2

vector<int>& is a reference to a vector, vector<int&> is vector of int reference, in fact vector<int&> would simply not compile, more detail: Why can't I make a vector of references?

Community
  • 1
  • 1
Rico Wang
  • 89
  • 6
0

Without the & a copy of the vector will be passed to the function.
= A copy of all elements. Changes / inserts/ deletes ... won´t affect the original vector.
(If you have a vector of pointers or something like that, they will still point to the same thing, but the pointers itself are copied too).

With the & you´ll have the same vector in the function, and all modifications will be visible to the outside too. Additionally, it´s probably faster not to copy everything if it isn´t nessecary (probably because of the whole move semantics & co. topic).

deviantfan
  • 11,268
  • 3
  • 32
  • 49
0

It is a reference, where the object being referenced has type vector<int> .

This is just the same as any other reference, apart from the type. If you are unfamiliar with references, they are a way of providing multiple labels for the same variable:

int a = 5;
int &b = a;

// now "a" and "b" are both labels for the same variable. any
// expression involving `a` can have that exchanged for `b`
// without making any difference.

a = 6;
std::cout << b << std::endl;   // 6

In the case you are talking about:

 void func( vector<int> &b ) { b.push_back(6); }

 int main()
 {
      vector<int> a;
      func(a);
      std::cout << a[0] << std::endl;     // 6
 }

it means that again a and b are both labels for the same vector.

I completely understand vector<int&>

vector<int&> is illegal, so hopefully your "complete understanding" of this is that it is illegal. It is not possible to have arrays or containers of references.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

When a function accepts a "vector&" it is said to be passed by reference. Suppose your vector reference parameter is named vec. Let's assume that you want to assign 0 to all the elements in your vector of ints. You may type:

int assign_zero(vector<int> vec){

    for(auto &x : vec){
    x = 0;
    }
}

With this, you may type something such as this in your main method:

originalVector = assign_zero(originalVector);

You could also accomplish the same thing using a reference to a vector in your function(notice the function is now void):

void assign_zero(vector<int>& vec){
    for(auto &x : vec){
    x = 0;
    }
}

Now, instead of a new vector of zeros being created from originalVector in memory and then assigned back to originalVector, the function assign_zero() modifies the vector directly. This allows you to use a function call like this where no extra vector is created:

assign_zero(originalVector);

Be aware that the function with the vector reference does not need to be void.