Automatic Type Deduction
Although C++ is a statically typed language, you can use auto
to let the compiler deduce the type of a variable during initialization. This feature was introduced in C++11 and is not available in older versions.
main.cpp
12345678910#include <iostream> int main() { auto num = 9; auto str = "I am string"; std::cout << num << std::endl; std::cout << str << std::endl; }
While this can save time and make the code more concise, it can also obscure the intended type, making errors harder to spot.
main.cpp
1234567891011#include <iostream> int main() { auto num1 = 9; auto num2 = 12; auto num3 = 15000; auto calculations = num1 / num2 * num3; std::cout << calculations; }
Specifying types would help us understand code better and find what's wrong:
main.cpp
1234567891011#include <iostream> int main() { float num1 = 9; float num2 = 12; float num3 = 15000; float calculations = num1 / num2 * num3; std::cout << calculations; }
Note
Any of
num1
,num2
ornum3
should befloat
to output the correct result.
Nevertheless, auto
is widely used in range-based for
loops since it is a versatile method of iterating over arrays and other containers. Just use the following syntax for iterating over arr
:
main.cpp
auto.h
123456789#include <iostream> int main() { int num_arr[5] = {1, 5, 10, 15, 20}; for (auto num : num_arr) std::cout << num << " "; }
It also can be used when the type is long to type but is clear from the context and can't lead to incorrect results.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Awesome!
Completion rate improved to 4.35
Automatic Type Deduction
Desliza para mostrar el menú
Although C++ is a statically typed language, you can use auto
to let the compiler deduce the type of a variable during initialization. This feature was introduced in C++11 and is not available in older versions.
main.cpp
12345678910#include <iostream> int main() { auto num = 9; auto str = "I am string"; std::cout << num << std::endl; std::cout << str << std::endl; }
While this can save time and make the code more concise, it can also obscure the intended type, making errors harder to spot.
main.cpp
1234567891011#include <iostream> int main() { auto num1 = 9; auto num2 = 12; auto num3 = 15000; auto calculations = num1 / num2 * num3; std::cout << calculations; }
Specifying types would help us understand code better and find what's wrong:
main.cpp
1234567891011#include <iostream> int main() { float num1 = 9; float num2 = 12; float num3 = 15000; float calculations = num1 / num2 * num3; std::cout << calculations; }
Note
Any of
num1
,num2
ornum3
should befloat
to output the correct result.
Nevertheless, auto
is widely used in range-based for
loops since it is a versatile method of iterating over arrays and other containers. Just use the following syntax for iterating over arr
:
main.cpp
auto.h
123456789#include <iostream> int main() { int num_arr[5] = {1, 5, 10, 15, 20}; for (auto num : num_arr) std::cout << num << " "; }
It also can be used when the type is long to type but is clear from the context and can't lead to incorrect results.
¡Gracias por tus comentarios!