Problems
If you have perfectly understood the range-based for loop, let's also understand auto, which we will learn now.
auto means "automatic" in English. In other words, it automatically infers and matches the data type!
Let's look at the code below.
int main(){
auto three = 3; // the data type of three is int
auto pi = 3.14; // the data type of pi is double
auto str = "apple"; // the data type of str is const char* ( i.e., it cannot be modified because it is const )
}If you set the data type of the variable three to auto and assign 3,
the computer automatically sets the data type of three to int because the assigned value 3 is an integer (int)!
Wouldn't it be very convenient if used well?
Like this, auto automatically matches the data type of the variable to the data type of the value assigned on the right.
However, there is something to be careful about.
int main(){
auto a; // Compile Error! Cannot infer data type because there is no assigned value
a = 3; // Compile Error already occurred in the line above... assigning later is useless
auto pi = 5; // the data type of pi is matched to int
pi = 3.14; // since pi is already an int, only 3 is stored
}Like the first example 'a', if you only declare with auto and don't assign a value immediately, a compile error occurs.
This is because the data type cannot be matched since there is no assigned value.
In the second example, since 5 was assigned to pi, the data type of pi automatically becomes int.
Therefore, even if you put 3.14 into pi later, only 3 is stored because pi is already an int.
Using auto makes various tasks very convenient.
First, a basic range-based for loop can also be written with auto.
In the code below, the data type of element automatically becomes int. This is because the data type of the assigned array A is int.
int A[5] = {1, 2, 3, 4, 5};
for(auto element : A){ // the data type of element is automatically int
printf("%d ", element);
}What if we apply this to the pair data type array we learned earlier?
In the code below, the data type of element will automatically be pair<int, int>.
Therefore, if you want to see the first element, use element.first, and if you want to see the second element, use element.second.
pair <int, int> A[3] = { {1, 2}, {3, 4}, {5, 6} };
for(auto element : A){ // the data type of element is automatically pair <int, int>
printf("%d %d\n", element.first, element.second);
}
// Output result
1 2
3 4
5 6However, the important point here is that since the data type of A is pair, it is always an ordered pair in the form of (variable, variable).
It is also possible to automatically separate this into two variables using auto!
Let's look at the code below and understand.
pair <int, int> A[3] = { {1, 2}, {3, 4}, {5, 6} };
for(auto [x, y] : A){ // x automatically becomes the first number, and y automatically becomes the second number.
printf("%d %d\n", x, y);
}
// Output result
1 2
3 4
5 6If you specify [x, y] using square brackets,
x and y automatically become the values of the ordered pair contained in array A.
In other words, whereas earlier element = {1, 2} at first,
this time [x, y] = {1, 2} at first. Automatically, x = 1 and y = 2.
You don't have to attach .first and .second like in the first code!
The same applies to the array data type. You can use a single variable element as in the code below, or separate variables with [x, y, z].
array <int, 3> A[3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for(auto element : A){
printf("%d %d %d\n", element[0], element[1], element[2]);
}
for(auto [x, y, z] : A){
printf("%d %d %d\n", x, y, z);
}It also applies to structures. Let's run this code.
#include <stdio.h>
struct Data{
int x, y;
double z;
};
int main(){
Data arr[3] = { { 1, 2, 0.1 }, { 3, 4, 0.2 }, { 5, 6, 0.3 } };
for(auto element : arr){ // the data type of element automatically becomes Data.
printf("%d %d %.1f\n", element.x, element.y, element.z);
}
printf("\n");
for(auto [a, b, c] : arr){ // the data types of a and b automatically become int, and the data type of c automatically becomes double
printf("%d %d %.1f\n", a, b, c);
}
}If you use a single variable element like in the first for loop, the data type of element automatically becomes Data,
and if you use 3 variables [ a, b, c ] like in the second for loop, the three variables x, y, and z contained in Data are stored in a, b, and c in order.
Precautions
However, a small point to be careful about is that when modifying the values of an array, such as when receiving input, you must attach &,
and like for ( auto &[a, b, c] : arr ), when variables are separated, the & symbol must be placed before the square brackets.
Alternatively, you can reference with a single variable like for ( auto &element : arr ).
<Application Problem>
Now let's use this to solve a problem.
Let's create an array that holds 10 string name and int age.
Receive their information as input,
and 1st priority: descending order of age, 2nd priority: ascending order of string (lexicographical order)
output the sorted results.
For learning purposes, when using a for loop, be sure to use a range-based for loop utilizing auto.
Input
Name and age are entered over 10 lines.
Output
Sort based on the criteria specified in the problem and output the result.
Example
mbappe 26
haaland 24
ronaldo 40
vinicius 24
messi 38
yamal 18
sancho 24
alvarez 24
hakimi 26
suarez 38
ronaldo 40
messi 38
suarez 38
hakimi 26
mbappe 26
alvarez 24
haaland 24
sancho 24
vinicius 24
yamal 18