Created
July 11, 2014 18:21
-
-
Save dblalock/93a98b481e16baeea2be to your computer and use it in GitHub Desktop.
C++ formatted width output stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class formatted_output | |
{ | |
private: | |
int width; | |
ostream& stream_obj; | |
public: | |
formatted_output(ostream& obj, int w): width(w), stream_obj(obj) {} | |
template<typename T> | |
formatted_output& operator<<(const T& output) | |
{ | |
stream_obj << setw(width) << output; | |
return *this; | |
} | |
formatted_output& operator<<(ostream& (*func)(ostream&)) | |
{ | |
func(stream_obj); | |
return *this; | |
} | |
}; | |
You can now call it like the following: | |
formatted_output field_output(cout, 10); | |
field_output << x << y << endl; | |
//http://stackoverflow.com/a/7248697 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment