lundi 20 juillet 2020

dereferenced iterator in range based for loop [duplicate]

In this code:

string s = "hello";

for(auto i : s)
{
    i = 'p';
    cout<<i<<'\n';
} 
cout<<s<<endl;

the output will be:

p
p
p
p
p
hello

So when i is used to refer to the next element in string s it is not an iterator but a dereferenced iterator? Had it been auto &i it would certainly have changed the value of string s pointing that it is dereferenced iterator. Is it correct?

Aucun commentaire:

Enregistrer un commentaire