How does using stringstream
to extract an integer value from a string differ from simply using explicit value casting to change the type?
Example:
string a = "1234";
int x;
x= int (a);
vs.
string a = "1234";
int x;
stringstream (a) >> x;
How does using stringstream
to extract an integer value from a string differ from simply using explicit value casting to change the type?
Example:
string a = "1234";
int x;
x= int (a);
vs.
string a = "1234";
int x;
stringstream (a) >> x;
Well, major difference is that:
string a = "1234";
int x;
x= int (a);
does not compile, there is no conversion from std::string to int. Modern way to convert std::string to int is to use stoi function, but be carefull with it because it will throw for "x1234" but will hapilly parse: "1234x".