What is the auto keyword in C++?
#include <iostream>
#include <map>
#include <string>
#include <vector>
int main() {
auto n = 5; // int
auto d = 2.5; // double
auto name = std::string("C++");
std::vector<std::map<std::string, int>> data = {{{"a", 1}}};
for (const auto& m : data) { // no long type name
std::cout << m.size() << '\n';
}
std::cout << n << ' ' << d << ' ' << name << '\n';
}
Output
1
5 2.5 C++
autoneeds an initial value.autoremovesconstand references: useconst auto&when you need them.
See C++ Variables.
Learn more about the similar topics:
| Tutorials |
|---|
| C++ Variables |
| C++ Data Types |
| C++ Type Conversion and Casting |
| C++ Operators |
| Exercises & Assignments |
|---|
| Print your name and age with variables |
| Convert Celsius to Fahrenheit |
| Interview Questions & Answers |
|---|
| What is the auto keyword in C++? |
| What is the difference between const and constexpr in C++? |