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++

  • auto needs an initial value.
  • auto removes const and references: use const auto& when you need them.

See C++ Variables.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.