We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

C++11 ‘auto’ Tutorial and Examples

In this article we will learn how to use auto variable in C++11.


auto is introduces in C++11. With auto we can declare a variable without specifying its type. Its type will be deduced by the data to which its initializing i.e.

// Storing a int inside a auto variable
auto var_1 = 5;

// Storing a character inside a auto variable
auto var_2 = 'C';

std::cout<<var_1<<std::endl;
std::cout<<var_2<<std::endl;

Here type of var_1 will be int and type of var_1 will be char.

Advertisements

[showads ad=inside_post]

 

We can story ant type in auto even if its a function or some iterator. Storing Lambda function inside a auto variable,

// Storing Lambda function inside a auto variable
auto fun_sum = [](int a , int b){
					return a+b;
				};

std::cout<<fun_sum(4,5)<<std::endl;

Main advantage of auto comes with types that are long to write i.e. Suppose we have a std::map of std::string & std::string i.e.

std::map<std::string, std::string> mapOfStrs;

// Insert data in Map
mapOfStrs.insert(std::pair<std::string, std::string>("first", "1") );
mapOfStrs.insert(std::pair<std::string, std::string>("sec", "2") );
mapOfStrs.insert(std::pair<std::string, std::string>("thirs", "3") );

Now we want to iterate over the map and display its content using iterator. To declare an iterator of this kind of map we need to write,

// Iterate over the map and display all data;
// Create an iterator
std::map<std::string, std::string>::iterator it = mapOfStrs.begin();
while(it != mapOfStrs.end())
{
	std::cout<<it->first<<"::"<<it->second<<std::endl;
	it++;
}

Instead of typing std::map<std::string, std::string>::iterator  we can just use auto here i.e.

// Iterate using auto
auto itr = mapOfStrs.begin();
while(itr != mapOfStrs.end())
{
	std::cout<<itr->first<<"::"<<itr->second<<std::endl;
	itr++;
}

Important points about auto variable in C++11

1.) Once you have initialized the auto variable then you can change the value but you cannot change the type i.e.

auto x = 1;
// Cannot change the type of already initialized auto variable
// Error will occur at compile time
// x = "dummy";

2.) It cannot be left uninitialized i.e.

// Can not declare auto variable without initialization
// because its type is based on initiazing value.
//auto a;

Returning an auto from a function

To return an auto variable from a function we need to declare it in a special way i.e.

auto sum(int x, int y) -> int
{
	return x + y;
}

Calling this function that returns auto,

auto value = sum(3, 5);

Complete executable code is as follows,

#include <iostream>
#include <typeinfo>
#include <map>
#include <string>
#include <iterator>

auto sum(int x, int y) -> int
{
	return x + y;
}

int main()
{

	// Storing a int inside a auto variable
	auto var_1 = 5;

	// Storing a character inside a auto variable
	auto var_2 = 'C';

	std::cout << var_1 << std::endl;
	std::cout << var_2 << std::endl;

	// Storing Lambda function inside a auto variable
	auto fun_sum = [](int a , int b)
	{
		return a+b;
	};

	std::cout << fun_sum(4, 5) << std::endl;

	std::map<std::string, std::string> mapOfStrs;

	// Insert data in Map
	mapOfStrs.insert(std::pair<std::string, std::string>("first", "1"));
	mapOfStrs.insert(std::pair<std::string, std::string>("sec", "2"));
	mapOfStrs.insert(std::pair<std::string, std::string>("thirs", "3"));

	// Iterate over the map and display all data;
	// Create an iterator
	std::map<std::string, std::string>::iterator it = mapOfStrs.begin();
	while (it != mapOfStrs.end())
	{
		std::cout << it->first << "::" << it->second << std::endl;
		it++;
	}

	// Iterate using auto
	auto itr = mapOfStrs.begin();
	while (itr != mapOfStrs.end())
	{
		std::cout << itr->first << "::" << itr->second << std::endl;
		itr++;
	}

	auto x = 1;
	// Cannot change the type of already initialized auto variable
	// Error will occur at compile time
	// x = "dummy";

	// Can not declare auto variable without initialization
	// because its type is based on initiazing value.
	//auto a;

	auto value = sum(3, 5);

	std::cout << value << std::endl;
	return 0;
}

 

Cart

Your Cart is Empty

Back To Shop