C++primer Fifth Edition, the third chapter appears this procedure, asks to interpret
Attached Source:
Code Listing 1:
#include <iostream>
#include <string>
using namespace Std;
string s = "Hello";
for (auto &i:s)//i is a reference I exactly what is referenced?
i = ToUpper (i); Change to uppercase, affecting the value of s
cout<<s<<endl; The value of S is HELLO
--------------------------------------------------------------------------
Code Listing 2:
#include <iostream>
#include <string>
using namespace Std;
string s = "Hello";
for (auto i:s)//The book says I is char type, that s[n]?
i = ToUpper (i); Change to uppercase without affecting the value of s
cout<<s<<endl; The value of S is Hello
The type recognition of auto is only given to the compiler, which occurs in the compile phase, not the run phase.
for (auto &i:s)
Using the code above to understand &i=s[index] I is a reference to S[index], change I will change s
for (auto i:s)
Use the above code to understand Char I=s[index] to assign a value to I with S[index]
Auto (c++11)