We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The auto command just asks the compiler to figure out what data type it should be (char in this case). If you are iterating through a string, you are going to be using chars because that is what a string is made up of. This would produce the same result, and be more clear:
for(char& c : str) {
do_things_with(c);
}
You can do the same thing to iterate thorough an array of ints for example:
int arr[4] = {1,2,3,4};
for (int &i : arr) {
i = i + 1;
}
output: arr = {2,3,4,5}
Where as this:
int arr[4] = {1,2,3,4};
for (int i : arr) {
i = i + 1;
}
output: arr = {1,2,3,4} because it's not actually changing the array without the &
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Caesar Cipher
You are viewing a single comment's thread. Return to all comments →
The
auto
command just asks the compiler to figure out what data type it should be (char
in this case). If you are iterating through astring
, you are going to be usingchar
s because that is what astring
is made up of. This would produce the same result, and be more clear:You can do the same thing to iterate thorough an array of ints for example:
output:
arr = {2,3,4,5}
Where as this:
output:
arr = {1,2,3,4}
because it's not actually changing the array without the&