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 last for loop in this case is a newer feature in C++. This same type of loop is usually referred to as a for each or for in loop in other languages.
I will explain this loop in a more pseudo-code way, and then explain it in C++ terms.
Say you have an array of elements. Instead of initializing an integer and then incrementing that integer and indexing into the array to get access to the element, it would be great if we could just have access to the elements themselves when we go through the array.
If you had a row of kittens lined up and you wanted to pet them all, it'd be easy enough to say, "Give me the first kitten, please," then pet the first kitten, and then say, "Okay, give me the next kitten please." Notice how we don't care what index the next kitten is at, we just want the next kitten? Once we get to the end of the kitten row, we simply notice there's no more kittens left when we get the next one, and we quit petting the kittens.
Well in C++, we're use to iterating through arrays using indexes, instead of saying we just want the next element. But everything that I've just described above is very similar to what this last for loop does.
for(autoval:freq){count+=abs(val);}
If you don't know what the auto keyword is, I suggest doing some quick reading on that. It shouldn't be too complicated.
In this for loop, what we end up doing is iterating through each element of the vector freq. In this case we make a copy of each element, and then use that within the for loop. If we wanted to use a reference to the element instead of copying it each time, we could instead say:
for (auto &val: freq) {...
Once we've gone through all the elements in freq, we simply exit the for loop.
for(auto&val:freq){cou<<"val="<<val<<endl;}
If we had vector<int>freq = {2,4,6} (this exact initialization is good for C++11 onward), then when we iterate through the loop, we would get:
Strings: Making Anagrams
You are viewing a single comment's thread. Return to all comments →
The last
for
loop in this case is a newer feature in C++. This same type of loop is usually referred to as afor each
orfor in
loop in other languages.I will explain this loop in a more pseudo-code way, and then explain it in C++ terms.
Say you have an array of elements. Instead of initializing an integer and then incrementing that integer and indexing into the array to get access to the element, it would be great if we could just have access to the elements themselves when we go through the array. If you had a row of kittens lined up and you wanted to pet them all, it'd be easy enough to say, "Give me the first kitten, please," then pet the first kitten, and then say, "Okay, give me the next kitten please." Notice how we don't care what index the next kitten is at, we just want the next kitten? Once we get to the end of the kitten row, we simply notice there's no more kittens left when we get the next one, and we quit petting the kittens.
Well in C++, we're use to iterating through arrays using indexes, instead of saying we just want the next element. But everything that I've just described above is very similar to what this last
for
loop does.If you don't know what the
auto
keyword is, I suggest doing some quick reading on that. It shouldn't be too complicated.In this
for
loop, what we end up doing is iterating through each element of the vectorfreq
. In this case we make a copy of each element, and then use that within thefor
loop. If we wanted to use a reference to the element instead of copying it each time, we could instead say:for (auto &val: freq) {...
Once we've gone through all the elements in
freq
, we simply exit thefor
loop.If we had
vector<int>freq = {2,4,6}
(this exact initialization is good for C++11 onward), then when we iterate through the loop, we would get:I hope this helps!