C++ Competitive Programming : Effective use of Auto Keyword in C++ 11 and above

Hello coderz, Welcome to CodingBroz where you learn new tips and tricks about Competitive Programming in C++ and many more programming related stuffs.

Today I am going to give you some awesome tips and tricks of competitive programming and tell you about auto keywords available in C++ 11 and above, so that you can use them effectively and get an amazing edge over your competitive programming in C++ journey.

C++ Competitive Programming : Effective use of Auto Keyword in C++ 11 and above

Note : The auto keyword is only available for C++11 and above versions.

What is an auto keyword in C++ and how to use it effectively?

Type Inference means to automatically detect the datatype of an expression in a programming language and in C++ we use auto keyword for this purpose.

Before C++11, each data type needs to be declared explicitly at the compile time but in the new C++ versions many keywords are introduced which automatically detect the data type of the given variable.

Using this auto in C++11 and above works like a charm in competitive programming and it saves time and effort when you are doing competitive programming. 

Now, let’s see how to use auto effectively during competitive programming.

Using auto to declare Data types

Commonly in C++ data types are declared explicitly when you write code. Whenever you want to declare any integer type variable you need to use int keyword or if you want to declare a character type variable you need to use char keyword, but this can be easily done using auto keyword.

Lets see using an example :

We commonly declare variables in C++ like this :

int a = 10;
double d = 10.5 ;
char  ch = ‘a’ ;
string s = “hello coder” ;
int *p = &a ;

So, this is the way you declare variables in C++, but by using auto you can do the same like this :

auto a = 10; // It automatically declares integer type
auto d = 10.5 ; // It automatically declares double type
auto  ch = ‘a’ ; // It automatically declares character type
auto s = “hello coder” ; // It automatically declares string type
auto *p = &a ; // It automatically declares integer type pointer

These are basic uses of auto keyword, let’s see some more amazing uses of it which will make you feel like your mind is blown and why you were not aware of these tips and tricks of effectively using the power of new C++ versions in your competitive programming journey.

Using auto with iterators 

One of the best uses of auto keyword is when you use iterators while traversing vectors or maps in C++. While using iterators, making the use of auto keyword will be like a blessing to you as it will save a lot of time and effort when dealing with iterators.

We use iterators to iterate in vectors, maps etc. Let’s see how to traverse the vector and map using the traditional simple way.

Traversing Vector in C++

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    vector<int> ar = { 1, 2, 3, 4, 5 }; 
      
    // Declaring iterator to a vector 
    vector<int>::iterator ptr; 
      
    // Displaying vector elements using begin() and end() 
    cout << "The vector elements are : "; 
    for (ptr = ar.begin(); ptr < ar.end(); ptr++) 
        cout << *ptr << " "; 
    return 0;
}

Now, insteading of writing vector<int>::iterator ptr; a long iterator declaration we can use auto keyword.

Vector traversal using auto keyword

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    vector<int> ar = { 1, 2, 3, 4, 5 }; 

    // Displaying vector elements using begin() and end() 
    cout << "The vector elements are : "; 
    for (auto ptr = ar.begin(); ptr < ar.end(); ptr++) 
        cout << *ptr << " "; 
    return 0;
}

See, how we can skip the line vector<int>::iterator ptr; and instead use auto to automatically declare the type. It just saves so much effort and time.

Now, let’s see an example of a map in c++.

Traversing map in C++ (Simply)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  // show content:
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

    return 0;
}

Here, we need to initialise an iterator to traverse our map using std::map<char,int>::iterator it=mymap.begin(); code in the for loop.

Now, let’s see how we can effectively use auto keyword to get the above result. 

Traversing map in C++ (auto keyword)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  // show content:
  for (auto it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

    return 0;
}

Using auto keywords we don’t need to write a long line to declare a map iterator and we can easily save our time and effort.

Instead of this : std::map<char,int>::iterator it=mymap.begin();

Use this : auto = mymap.begin();

So, always use auto and avoid writing a long code to initialize iterators and save time to get an extra edge in competitive programming. In competitive programming each second counts!

Extra Tip#1

In the above code we used :

ios_base::sync_with_stdio(false);
             cin.tie(NULL);

These 2 lines for faster Input and Output. This is known as Fast I/O.

It is very essential in competitive programming. We have covered this topic in detail. You can read it from here, “Fast I/O in C++ for Competitive programming”.

Leave a Comment

Your email address will not be published. Required fields are marked *