- The auto keyword is a C++11 feature.
- Note:- Use "g++ -std=c++0x filename.cpp" to enable this feature.
- The compiler is able to determine the type of a variable from its initialization.
- With the advent of template types and template metaprogramming techniques, the type of something, particularly the well-defined return value of a function, may not be easily expressed. C++11 auto keyword provides a mitigation for this.
- auto is also useful for reducing the verbosity of the code.
Demonstrate the usage of "auto" keyword
#include <iostream>
#include <map>
using namespace std;
int main() {
auto x = 100; // type of x is int
map<int, string> employees;
employees[100] = "John";
map<int, string>::iterator iter1 = employees.begin(); // More verbose, prior to C++ 11
auto iter2 = employees.begin(); // In C++ 11 using auto
return 0;
}
0 comments:
Post a Comment