Usage of T vs auto for variable declaration

why cant we use T instead of auto in the first loop inside print()?

Hello @rohanmd07,

  1. This is because the type of adjList is map not T.
  2. It contains the element of of generic type.
  3. To iterate over the elements of the map you are required to create an iterator of type map as you do in case of vectors and other STL data-structures.

So, you are using auto which will implicitly create an iterator of an appropriate type at compile time. Thus reducing our efforts.

Hope, this would help.
Give a like if you are satisfied.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.