C++, iostream - own manipulator dependent | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

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; }

6th Feb 2018, 8:34 PM
Łukasz Szczyglewski
Łukasz Szczyglewski - avatar
4 Answers
+ 2
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.
6th Feb 2018, 9:56 PM
---
--- - avatar
+ 6
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.)
6th Feb 2018, 9:06 PM
John Wells
John Wells - avatar
+ 4
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.
6th Feb 2018, 11:03 PM
Łukasz Szczyglewski
Łukasz Szczyglewski - avatar
+ 1
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
7th Feb 2018, 4:44 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar