Page not loading? Try clicking here.
Placeholder

#8559

Tutorial: range-based for loop 2s 1024MB

Problems

The code to print an array A of size 5 from the beginning (index 0) to the end (index 4) is as follows.

It's a very, very simple code.

int A[5] = {10, 20, 30, 40, 50};

for(int i = 0; i < 5; i++){
    printf("%d ", A[i]);
}

What we will learn this time is a slightly different form of the for loop.

Let's talk while looking at the code directly.

int A[5] = {10, 20, 30, 40, 50};

for(int element : A){
    printf("%d ", element);
}

The code above works exactly the same as the first code. Only the form of the for loop is slightly different.

This form of the for loop is called a range-based for loop. (It is a syntax introduced since C++11.)

Always specify the data type at the very beginning. ( int, double, char ... etc. )

Since the data type of array A we want to look at was int, we used int accordingly.

After that, write the variable name and put a colon ( : ) next to it. (It's not the semi-colon ( ; ) we use at the end of each line!!)

Finally, write the container name we want to examine.

A container is a data type that can hold elements, such as arrays, Maps, Vectors, Sets, etc. (You will learn all of these later!!)

If you write A like in the example above, it means you will examine all elements of array A in order.

Then, element becomes the value of the element itself contained in array A.

What this means is, in the example above,

First, 10, which is the value of A[0], is stored in element.

Then, 20, which is the value of A[1], is stored in element,

and then 30, 40, and 50 are stored in element in sequence.

Once the last element is stored in element, the for loop ends automatically.

Therefore, if you print element as in the code above, the elements of array A are printed in order from beginning to end.

(Output result: 10 20 30 40 50)


What if we write the code like this? Will array A change? Let's think about it.

int A[5] = {10, 20, 30, 40, 50};

for(int element : A){
    element += 5;
}

To conclude, no.

Even if you added 5 to each element, the original array A remains [10, 20, 30, 40, 50].

Why is that?

It's because element "copies" the value from array A.

Changing element does not change array A. This is because element is just a "copy".

Then, what should we do to make array A change?

You just need to put & in front of element.

Similar to the concept of pass by reference learned in the function chapter of Self-Directed C Programming,

element works as the original itself of array A, not a copy.

Therefore, you must write the code with &element as shown below for array A to actually change to [15, 25, 35, 45, 55].

int A[5] = {10, 20, 30, 40, 50};

for(int &element : A){ // Note the & symbol!
    element += 5;
}

Using this, both input and output are possible with range-based for loops.

When inputting, you can actually modify array A by attaching & in front of element.

When outputting, since you are only reading the values of array A and not modifying them, there's no need to attach &. (It doesn't matter if you do, though.)

int A[5];

for(int &element : A){
    scanf("%d", &element);
}

for(int element : A){
    printf("%d ", element);
}


Below are some notes. Things to be careful about when using range-based for loops.

* You cannot use an already declared variable and omit the data type!

int element;
for(element : A){  // Error !!
    printf("%d ", element);
}

As in the code above, if you declare element in advance and write it while omitting int, a compile error occurs.

You must declare a new variable inside the for loop. (Specifying the data type is mandatory.)

* It will always iterate through the array from the very beginning to the end.

You cannot make it start from a specific index of the array and look only up to a specific index.

In other words, if there is an array A, you cannot make it start from A[1]. This is because it always starts from A[0].

Therefore, if you want to receive 10 elements as input, you must create array A with exactly 10 slots.


Now, let's solve the problem below by applying range-based for.

Input 10 integers, add 1 to each number, and print the sorted result. (Use the STL Sort function for sorting)

When using for loops, make sure to solve it using only the range-based for loop you just learned.


Input

10 integers are input in order. ( 0 or more and 10,000 or less )


Output

Sort and output the results of adding 1 to each integer.


Example

7 8 3 7 2 10 3 0 3 4
1 3 4 4 4 5 8 8 9 11


Source

againalgo

You must sign in to write code.