As for education, as you say, I will show you how I will do such a thing. Otherwise, stringstream is really the way to go.
It looks like you want to create a streambuf implementation that is then written to the / deque vector. Something like this (copying from another answer from me that targets the / dev / null stream ):
template<typename Ch, typename Traits = std::char_traits<Ch>, typename Sequence = std::vector<Ch> > struct basic_seqbuf : std::basic_streambuf<Ch, Traits> { typedef std::basic_streambuf<Ch, Traits> base_type; typedef typename base_type::int_type int_type; typedef typename base_type::traits_type traits_type; virtual int_type overflow(int_type ch) { if(traits_type::eq_int_type(ch, traits_type::eof())) return traits_type::eof(); c.push_back(traits_type::to_char_type(ch)); return ch; } Sequence const& get_sequence() const { return c; } protected: Sequence c; };
You can use it as follows:
seqbuf s; std::ostream os(&s); os << "hello, i'm " << 22 << " years old" << std::endl; std::vector<char> v = s.get_sequence();
If you want to have a deck as a sequence, you can do this:
typedef basic_seqbuf< char, char_traits<char>, std::deque<char> > dseq_buf;
Or something like that ... Well, I have not tested it. But perhaps this is also good, so if there are more errors in it, you can try to fix them.
source share