manipulators

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

风格不统一 提交于 2020-01-10 04:53:06
问题 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

Demonstration of noskipws in C++

久未见 提交于 2020-01-06 12:43:27
问题 I was trying out the noskipws manipulator in C++ and I wrote following code. #include <iostream> #include <sstream> #include <string> using namespace std; int main() { string first, middle, last; istringstream("G B Shaw") >> first >> middle >> last; cout << "Default behavior: First Name = " << first << ", Middle Name = " << middle << ", Last Name = " << last << '\n'; istringstream("G B Shaw") >> noskipws >> first >> middle >> last; cout << "noskipws behavior: First Name = " << first << ",

Demonstration of noskipws in C++

≡放荡痞女 提交于 2020-01-06 12:43:14
问题 I was trying out the noskipws manipulator in C++ and I wrote following code. #include <iostream> #include <sstream> #include <string> using namespace std; int main() { string first, middle, last; istringstream("G B Shaw") >> first >> middle >> last; cout << "Default behavior: First Name = " << first << ", Middle Name = " << middle << ", Last Name = " << last << '\n'; istringstream("G B Shaw") >> noskipws >> first >> middle >> last; cout << "noskipws behavior: First Name = " << first << ",

How do stream manipulators with arguments work?

蓝咒 提交于 2020-01-01 04:57:10
问题 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

Overload handling of std::endl?

微笑、不失礼 提交于 2019-12-27 12:17:25
问题 I want to define a class MyStream so that: MyStream myStream; myStream << 1 << 2 << 3 << std::endl << 5 << 6 << std::endl << 7 << 8 << std::endl; gives output [blah]123 [blah]56 [blah]78 Basically, I want a "[blah]" inserted at the front, then inserted after every non terminating std::endl ? The difficulty here is NOT the logic management, but detecting and overloading the handling of std::endl . Is there an elegant way to do this? Thanks! EDIT: I don't need advice on logic management. I need

Effect of noskipws on cin>>

怎甘沉沦 提交于 2019-12-17 19:44:56
问题 As I understand, the extraction operator skips the whitespace in the beginning and stops upon encountering a whitespace or end of stream. noskipws can be used to stop ignoring the leading whitespaces. I have the following program where I have used noskipws. #include <iostream> using namespace std; int main() { char name[128]; cout<<"Enter a name "; cin>>noskipws>>name; cout<<"You entered "<<name<<"\n"; cout<<"Enter another name "; cin>>name; cout<<"You entered "<<(int)name[0]<<"\n"; return 0;

no match for ‘operator<<’ for std::endl after overload

早过忘川 提交于 2019-12-11 10:38:53
问题 I am sorry that I duplicate this question, but I don't have the reputation required to comment there and the answers there are not convincing for me. #include<iostream> class my_ostream : public std::ostream { public: std::string prefix; my_ostream():prefix("*"){} my_ostream& operator<<(const std::string &s){ std::cout << this->prefix << s; return *this; } }; int main(){ my_ostream s; std::string str("text"); s << str << std::endl; } Here I get: no match for ‘operator<<’ in ‘s.my_ostream:

How pass std::endl to a function and use it?

≯℡__Kan透↙ 提交于 2019-12-11 08:38:17
问题 I want to figure out how to pass a manipulator like std::endl to a function and then use the passed-in manipulator in the function. I can declare the function like this: void f(std::ostream&(*pManip)(std::ostream&)); and I can call it like this: f(std::endl); That's all fine. My problem is figuring out how to use the manipulator inside f . This doesn't work: void f(std::ostream&(*pManip)(std::ostream&)) { std::cout << (*pManip)(std::cout); // error } Regardless of compiler, the error message

Is my approach to a threadsafe log class awful?

假装没事ソ 提交于 2019-12-10 19:48:53
问题 I've been looking around at various approaches to the problem of threadsafe logging, but I haven't seen anything quite like this, so I dunno if it's somehow awful that I haven't noticed due to being a complete newbie to C++, threads and iostreams. It seems to work in the basic tests I've put it through. Basically I have a Log class (creative, I know...) which has operator<< set up for the standard manipulators, so I can merrily pass in whatever I want. However, I am aware that something like: