Another interesting new feature of C++11 standard is range-based for loop.
This feature is simplification of usage of for loop going through each element of some container (ex. vector). For other programming languages we can use following syntax when we would like to iterate through all container elements:
Those constructions are very simple and rather self-understandable. However in C++ until now (C++03 standard) we had to use following syntax when we would like to go through each element of container:
Complicated, right? Especially for beginner user who is just starting learning of C++ and STL library. So, how to simplity it? C++11 helps us here :) It defines new syntax for dealing with 'for each' iteration called "range-based for loop". Instead of above vector iteration we can do something like this:
Definitely simpler, more understandable and easier to read, I think.
What is more when we do not know exact type of data in our container we can use auto type this way:
Range-based for loop should simplify iterating through container.
This feature is simplification of usage of for loop going through each element of some container (ex. vector). For other programming languages we can use following syntax when we would like to iterate through all container elements:
Those constructions are very simple and rather self-understandable. However in C++ until now (C++03 standard) we had to use following syntax when we would like to go through each element of container:
Complicated, right? Especially for beginner user who is just starting learning of C++ and STL library. So, how to simplity it? C++11 helps us here :) It defines new syntax for dealing with 'for each' iteration called "range-based for loop". Instead of above vector iteration we can do something like this:
Definitely simpler, more understandable and easier to read, I think.
What is more when we do not know exact type of data in our container we can use auto type this way:
Range-based for loop should simplify iterating through container.
Comments
Post a Comment