Skip to content

Instantly share code, notes, and snippets.

@dazfuller
Created November 6, 2011 20:51
Show Gist options
  • Save dazfuller/1343463 to your computer and use it in GitHub Desktop.
Save dazfuller/1343463 to your computer and use it in GitHub Desktop.
Ignoring the Voices: C++11 Example - Using auto
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void cpp_old()
{
std::vector<std::string> my_collection;
my_collection.push_back("Hello");
my_collection.push_back("World");
for (std::vector<std::string>::iterator it = my_collection.begin(); it != my_collection.end(); ++it)
{
cout << *it << endl;
}
}
void cpp_new()
{
std::vector<std::string> my_collection;
my_collection.push_back("Hello");
my_collection.push_back("World");
for (auto it = my_collection.begin(); it != my_collection.end(); ++it)
{
cout << *it << endl;
}
}
int main()
{
cout << "Without using auto keyword" << endl;
cpp_old();
cout << "Using auto keyword" << endl;
cpp_new();
}
@dazfuller
Copy link
Author

Compiled as follows:

g++ -std=c++0x -Wall -Werror -o using_auto using_auto.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment