I'm going through a C++ book (cpluplusnotes for professionals), and got to this example and was wondering whats happening with num1 / 2 here and how the in(xxxx) is affecting it ?
Thankyou !
`#include <sstream>
...
std::istringstream in("10 010 10 010 10 010");
int num1, num2;
in >> std::oct >> num1 >> num2;
std::cout << "Parsing \"10 010\" with std::oct gives: " << num1 << ' ' << num2 << '\n';
// Output: Parsing "10 010" with std::oct gives: 8 8
in >> std::dec >> num1 >> num2;
std::cout << "Parsing \"10 010\" with std::dec gives: " << num1 << ' ' << num2 << '\n';
// Output: Parsing "10 010" with std::oct gives: 10 10
in >> std::resetiosflags(std::ios_base::basefield) >> num1 >> num2;
std::cout << "Parsing \"10 010\" with autodetect gives: " << num1 << ' ' << num2 << '\n';
// Parsing "10 010" with autodetect gives: 10 8
std::cout << std::setiosflags(std::ios_base::hex |
std::ios_base::uppercase |
std::ios_base::showbase) << 42 << '\n';
// Output: OX2A`
Also to note i understand Oct, dec , hex and all that i just dont get how num1/2 have been assigned values off the in
std::oct
and std::dec
tell the stream how to convert the string to an integer.
If one of them is set, then num1
and num2
are read as if they were written in base 8 or 10.
When none is set, 10
is read as decimal (default for digit-strings not stariting with 0) and 010
is read as octal (default for digit-strings starting with 0).