example1_basic.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <iostream>
using namespace std;

int main() {
    const int& x = 10;
    auto y = x;  // y is automatically deduced to be of type int

    // cout << "typeid(x): " << endl;
    cout << "typeid(x): " << typeid(x).name() << endl;

    std::vector<int> vec = {1, 2, 3, 4, 5};
    auto it = vec.begin();  // it is of type std::vector<int>::iterator

    for (auto elem : vec) {  // elem is deduced to be of type int
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}
Back to auto