Wednesday, June 22, 2011

A bit of C++0x auto keyword

Other cool shortcut in c++x0 is auto keyword it is wrist saver, but don't abuse it. If abused auto could make code unreadable like c# var.

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> vec = { 10, 20, 30, 40 };

    // Compare with pre c++x0 version
    //for(vector<int>::iterator it = vec.begin(); it != vec.end(); it++)
    for(auto it = vec.begin(); it != vec.end(); it++)
    {
        std::cout << *it << std::endl;
    }

    return 0;
}