Doubt in Map iterator

This problem is leetcode 1365.
In the line i have commented, could you tell me why am I having to write pair<const int,int> and using pair<int,int> is
giving error ?

Hey @nidhigupta847

By default, the compiler cannot bind a non-const or volatile lvalue reference to an rvalue.

Example

int& a = 2; // error
const int& b = 1; // ok

In this example, the variable a is a non-const lvalue reference. The compiler cannot bind a to the temporary initialized with the rvalue expression 2, and issues an error message. The variable b is a nonvolatile const lvalue reference, which can be initialized with the temporary initialized with the rvalue expression 1.

Similarly here for(pair<const int,int> &i: m) rvalues are temporary

I’m not being able to understand

When right side value is temporary then we always have to use const on left hand side so that we dont try to change its value later

Similarly when we use for loop like this
for(auto i:vect)
Here extraction of data from vect create temporary data
So we have to use const on LHS