Please tell me what role the ampersand plays in the parameter of this function because the program works correctly as with it and without it
# include & lt; iostream & gt;
#Include & lt; vector & gt;
Using Namespace STD;
TypeDef vector & lt; int & gt; T_summands;
void print_summands (int n_start, const t_summands & amp; Summands)
{
COUT & LT; & LT; N_START & LT; & LT; "=";
For (T_SUMMANDS :: const_terator Summands_it = Summands.begin (); Summands_it! = Summands.end (); ++ Summands_it)
{
COUT & LT; & LT; * Summands_it & lt; & lt; (Summands_it! = Summands.end () - 1? "+": "\ n");
}
}
Answer 1, Authority 100%
For Understanding – Well, consider that when transferring the link you simply pass the pointer, but you do not need to retire it all the time.
type
void f (int * x) {* x = * x * 2; }
...
int y = 4;
f (& amp; y);
Same through the link –
void f (int & amp; x) {x = x * 2; }
...
int y = 4;
f (y);
This is, of course, approaching, but for understanding it is suitable.
When you pass without a reference, the value is created – a copy is created. About this:
void f (int x) {x = x * 2; }
It’s like if (inaccurately! Just for understanding !!)
void f (int * y)
{
int x;
x = * y;
x = x * 2;
}
on the “domestic” level – the value transmitted does not change what is transmitted, according to the link – the transmitted variable itself changes.
void f (int & amp; x) {x = x * 2; }
...
int y = 4;
f (y); // Now y == 8
Void F (int x) {x = x * 2; }
...
int y = 4;
f (y); // Y == 4 ...