Q:

stringstream removes leading 0s

In the following code:   
stringstream ss;
int a = 0;
ss << str;
ss >> a;
cout << a;

If str = "05", stringstream removes the leading 0, and prints 5.
How can be this avoided?

A:

You are converting a string to an int. an int doesn't have any concept of leading zeros, it's just a number. If you want to print leading zeros in a stream, you might be interested in the setfill and setw manipulators. The following code will print a leading 0 if a is only one digit long.
cout << setw(2) << setfill('0') << a;

