C++, iostream - own manipulator dependent | Sololearn: Learn to code for FREE!


C++, iostream - own manipulator dependent

Hello, I wish to create own manipulator dependent on other manipulators or settings. I don't mean about parameter. I just want to get information from introduced stream. For example how to poses value (5) from cout<<setw(5)<<MyOwnManip<<endl; in MyOwnManip? Is it possible? ostream & MyOwnManip(ostream & mnpl) { int count; //... how to get data from whole stream? mnpl<<"you set width :"<<count<<endl; return mnpl; }

2/6/2018 8:34:16 PM

Łukasz Szczyglewski

4 Answers

New Answer


all these setter functions like setw(...) have usually also a corresponding getter function like width(). If you want to access the data itself the stringstream stuff is exactly what you are looking for, but in general its kinda against the idea of a stream, where you process data only piece by piece.


If I wanted to do this, I'd create my own class as a subclass of ostream. However, you might have hooks built in to ostream to help you do what you want. Read the documentation to see if there are callbacks available. (callbacks are your functions, which you ask ostream to call when some event occurs.)


Thanks. I was trying to copy ostream to ostringstream and do something with ostringstream.str(). I was wrong. Mostly manipulators have counterparts which answer what is set.


Maybe you can have a look at a. similar post which may have other answers you are looking for. https://www.sololearn.com/discuss/914304/?ref=app