C++ outputing to two streams together
i wanna send data to both A+B in the one call
ostream A,B;
A << "g";
B << "g";
how can i do it, ta zed
or even
template<typename T>void wrapper(ostream &a, ostream &b, const T& data){ a << data; b << data;}
struct DoubleStream { DoubleStream(ostream& a, ostream& b) : a_(a), b_(b) { } template<typename T> DoubleStream& output(const T& object) { a_ << object; b_ << object; return *this; }private: ostream& a_; ostream& b_;};template<typename T>DoubleStream& operator<<(DoubleStream& ds, const T& object) { return ds.output(object);}
Usage
#include "DoubleStream.h"#include <iostream>#include <complex>int main() { DoubleStream ds = DoubleStream(std::cout, std::err); ds << "Hello, World!" << ds << std::endl; ds << 5.5 << std::endl; ds << std::complex<double>(1.412, 16.5) << std::endl; return 0;}
or even
longer to write but shorter to use
class funcStreamer{ funcStreamer(ostream &a, ostream &b) : m_a(a), m_b(b) {} template <typename DATA_TYPE> void operator() (const DATA_TYPE& data) { m_a << data; m_b << data; }private: ostream &m_a; ostream &m_b}// in useostream a, b;funcStreamer outFunc(a, b);outFunc("string value");
longer to write but shorter to use
I thought I would be a little different and do something most likely not useful:
example usage:
[Edited by - snk_kid on May 25, 2005 6:46:10 PM]
#include <utility> // std::pair#include <iterator> // std::ostream_iterator, std::output_iterator_tag#include <iosfwd> // std::char_traitstemplate < typename Tp, typename CharT = char, typename Traits = std::char_traits<CharT> >struct pair_ostream_iterator : std::pair< std::ostream_iterator<Tp, CharT, Traits>, std::ostream_iterator<Tp, CharT, Traits> > { typedef CharT char_type; typedef Traits traits_type; typedef std::basic_ostream<CharT, Traits> ostream_type; typedef std::pair< first_type, second_type > pair_type; typedef std::output_iterator_tag iterator_category; typedef void value_type; typedef void difference_type; typedef void pointer; typedef void reference; typedef pair_ostream_iterator<Tp, CharT, Traits> Self; pair_ostream_iterator(ostream_type& out1, ostream_type& out2, const CharT* Delim = 0) : pair_type(first_type(out1, Delim), second_type(out2, Delim)) {} Self& operator=(const Tp& val) { first = val, second = val; return *this; } Self& operator*() { // pretend to return designated value return *this; } Self& operator++() { // pretend to preincrement return *this; } Self operator++(int) { // pretend to postincrement return *this; }};
example usage:
#include <cstddef> // std::size_t#include <cmath> // std::rand#include <algorithm> // std::generate_n, std::copy#include <iterator> // std::back_inserter#include <vector> // std::vector#include <iostream> // std::cout, std::cerrint main() { const std::size_t N = 10; std::vector<int> v; v.reserve(N); std::generate_n(std::back_inserter(v), N, std::rand); std::copy(v.begin(), v.end(), pair_ostream_iterator<int>(std::cout, std::cerr, "\n"));}
[Edited by - snk_kid on May 25, 2005 6:46:10 PM]
thanks for that but
sorry i should of been more specific i want to be able to (*)chain them together
thus only petewood's suggestion is applicable
but i get the following error
ds << std::flush;
error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'struct DoubleStream'
(*)able to input multiple data in one line eg string << float << string << flush;
sorry i should of been more specific i want to be able to (*)chain them together
thus only petewood's suggestion is applicable
but i get the following error
ds << std::flush;
error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'struct DoubleStream'
(*)able to input multiple data in one line eg string << float << string << flush;
looks like doublestream should be derived by the basic ostream or something.. no clue but a guess..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement