I know that `&` shows the memory where the file is located in, but why do we need that?
Not quite so.
The & operator returns the address of a variable. E.g. the following little program gets the pointer (memory address) of an integer and uses that pointer to change the value of the pointed variable.
#include <iostream>
int main()
{
int * pointerToMyAge = &myAge;
*pointerToMyAge = 22;
std::cout << "My age changed via a pointer: " << myAge << std::endl;
return 0;
}
However, this is not the case in your code. In your code the & operator has a different meaning, it means reference.
References are similar to pointers, but unlike pointers, they do not need to be dereferenced (e.g.: *pointerToMyAge) before you can read/write their value (and not where they point to).
So in the context void swap(string &x, string &y); the & operator tells the compiler that both x and y are not mere strings but references to strings.
We have to take a side step here... When you pass arguments to a function, you can do it in two ways: either as a value or as a reference. If you pass arguments as values, a copy is made. If you pass them as a reference, the original variable and the parameter of the function will not only have the same value, but they also reference the same memory address: changing one would change the other.
So by passing x and y as string references swap() can replace their values. You can easily check this by removing the & operator. The code will compile. It will run but at the end "Kool-aid\nWater" will be printed and not the other way around. However, inside swap() their values got replaced. But since this time we did not use references, just copies (value-based parameter passing) nothing "leaves" the function swap().
I hope this makes sense.
Good luck!