int main()
{
vector<int> v={1,2,4};
int &a=*v.begin();
cout<<a;
return 0;
}
In the above code segment, the dereferenced value of the iterator v.begin()
assigned to &a
. Printing a
displays 1
int main()
{
vector<int> v={1,2,4};
int a=*v.begin();
cout<<a;
return 0;
}
Here, the iterator is deferenced and the value is assigned to a
(without the &) instead. Printing a
still displays 1.
I'm still learning C++ and it doesn't make sense to me why the first code block works. What does int &a
signify?