September 2017
Beginner to intermediate
384 pages
9h 18m
English
Our "sticky std::hex" problem can be solved by saving and restoring the state of the ostream around each complicated output operation, or by creating a brand-new ostream each time we want to output something. An example of the former is as follows:
void test() { std::ios old_state(nullptr); old_state.copyfmt(std::cout); std::cout << std::hex << 225; // "e1" std::cout.copyfmt(old_state); std::cout << 42; // "42" }
An example of the latter is as follows:
void test() { std::ostream os(std::cout.rdbuf()); os << std::hex << 225; // "e1" std::cout << 42; // "42" }
Notice how convenient it is that the iostreams library separates the idea of a "streambuf" from the idea of a "stream"; in the preceding example, ...
Read now
Unlock full access