manipulators

Simple wostream logging class (with custom stream manipulators)

时光怂恿深爱的人放手 提交于 2019-12-10 18:07:56
问题 I've been reading tons of questions, articles, and documentation, but I've not found a solution to my problem. I'd like to create a simple class for use in debugging. The end result of which would allow me to do something like this: logger << error << L"This is a problem!" << endl; logger << warning << L"This might be a problem!" << endl; logger << info << L"This isn't a problem but I thought you should know about it" << endl; With the idea that within the logger class I can toggle whether or

How to implement custom sticky manipulator that automatically adds separators?

亡梦爱人 提交于 2019-12-09 22:57:41
问题 The print function in Python automatically separates its arguments with a customisable separator. Is there any way to emulate this behavior in C++ by using stream manipulators? That is, the following C++ code: std::cout << custom::sep(", ") << 1 << "two" << 3 << std::endl; Should work similar to the following Python code: print(1, "two", 3, sep=", ") The desired output would be: 1, two, 3 How would I go about implementing custom::sep ? It seems a bit more tricky than your standard custom

How do stream manipulators with arguments work?

点点圈 提交于 2019-12-03 14:19:34
In Stroustrup's C++ book, there is an example of a custom manipulator taking an argument (pls see the attached code). I am confused about how the struct is created. In particular, it looks like there are two int arguments for the constructor of "smanip", one for the function pointer "ff", one for "ii". I don't understand how the int argument is passed to create the structure by using: cout << setprecision(4) << angle; In addition, what is the order these functions get called, and how the the type arguments Ch and Tr are determined? Thanks a lot. // manipulator taking arguments struct smanip{

Custom manipulator for class

∥☆過路亽.° 提交于 2019-12-02 20:45:54
问题 I'm trying to write a stream manipulator with arguments. I have class with 3 int's CDate(Year, Month, Day). So I need to make manipulator date_format(const char*) . e.g. : CDate a(2006, 5, 15); cout <<"DATE IS : " << date_format("%Y-hello-%d-world-%m-something-%d%d") << a; Output will be : DATE IS : 2006-hello-15-world-5-something-1515 Guess i need to use that ios_base & dummy_date_format_manipulator ( ios_base & x ) { return x; } ios_base & ( * ( date_format ( const char * fmt ) ) )( ios

Custom manipulator for class

笑着哭i 提交于 2019-12-02 08:47:21
I'm trying to write a stream manipulator with arguments. I have class with 3 int's CDate(Year, Month, Day). So I need to make manipulator date_format(const char*) . e.g. : CDate a(2006, 5, 15); cout <<"DATE IS : " << date_format("%Y-hello-%d-world-%m-something-%d%d") << a; Output will be : DATE IS : 2006-hello-15-world-5-something-1515 Guess i need to use that ios_base & dummy_date_format_manipulator ( ios_base & x ) { return x; } ios_base & ( * ( date_format ( const char * fmt ) ) )( ios_base & x ) { return dummy_date_format_manipulator; } but i don't know how. You can use pword array for

C++ - How to reset the output stream manipulator flags [duplicate]

夙愿已清 提交于 2019-11-30 17:19:05
This question already has an answer here: Restore the state of std::cout after manipulating it 6 answers I've got a line of code that sets the fill value to a '-' character in my output, but need to reset the setfill flag to its default whitespace character. How do I do that? cout << setw(14) << " CHARGE/ROOM" << endl; cout << setfill('-') << setw(11) << '-' << " " << setw(15) << '-' << " " << setw(11) << '-' << endl; I thought this might work: cout.unsetf(ios::manipulatorname) // Howerver I dont see a manipulator called setfill Am I on the wrong track? Éric Malenfant Have a look at the Boost

Center text in fixed-width field with stream manipulators in C++

只愿长相守 提交于 2019-11-30 13:14:30
问题 I am refactoring some legacy code which is using printf with longs strings (without any actual formatting) to print out plain text table headers which looks notionally like this: | Table | Column | Header | which are currently being produced like this: printf("| Table | Column | Header |"); I would like to produce the above with code to the effect of 1 : outputStream << "|" << std::setw(10) << std::center << "Table" << "|" << std::setw(10) << std::center << "Column" << "|" << std::setw(9) <<

Center text in fixed-width field with stream manipulators in C++

若如初见. 提交于 2019-11-30 06:52:34
I am refactoring some legacy code which is using printf with longs strings (without any actual formatting) to print out plain text table headers which looks notionally like this: | Table | Column | Header | which are currently being produced like this: printf("| Table | Column | Header |"); I would like to produce the above with code to the effect of 1 : outputStream << "|" << std::setw(10) << std::center << "Table" << "|" << std::setw(10) << std::center << "Column" << "|" << std::setw(9) << std::center << "Header" << "|" << std::endl; which does not compile because <iomanip> has the stream

C++ - How to reset the output stream manipulator flags [duplicate]

ⅰ亾dé卋堺 提交于 2019-11-30 00:50:31
问题 This question already has answers here : Restore the state of std::cout after manipulating it (6 answers) Closed last year . I've got a line of code that sets the fill value to a '-' character in my output, but need to reset the setfill flag to its default whitespace character. How do I do that? cout << setw(14) << " CHARGE/ROOM" << endl; cout << setfill('-') << setw(11) << '-' << " " << setw(15) << '-' << " " << setw(11) << '-' << endl; I thought this might work: cout.unsetf(ios:

ostream showbase does not show “0x” for zero value

泪湿孤枕 提交于 2019-11-29 13:20:44
PSPS: (a Pre-scripted Post-script) It has just come to mind that a more prescient question would have included the notion of: Is this non-display of "0x"(showbase) for zero-value integers a standard behaviour, or is it just a quirk of my MinGW implementation? It all began on a pleasant Sunday morning... I want to dump some Handles in their hex representation, and in a consistant, formatted way. I want a leading 0x and a fixed width , but this is proving to be elusive using the expected stream manipulators. The only way I've found to do this is to cast the Handles to an unsigned long. This