creating an ostream

前端 未结 4 589
走了就别回头了
走了就别回头了 2021-01-05 08:09

I am trying to create a c++ ostream for educational reasons. My test will be creating an ostream that acts like a ofstream would except instead of writing to a file it would

相关标签:
4条回答
  • 2021-01-05 08:44

    As it is for education, as you say, i will show you how i would do such a thingy. Otherwise, stringstream is really the way to go.

    Sounds like you want to create a streambuf implementation that then writes to a vector / deque. Something like this (copying from another answer of me that targeted a /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;
    };
    
    // convenient typedefs
    typedef basic_seqbuf<char> seqbuf;
    typedef basic_seqbuf<wchar_t> wseqbuf;
    

    You can use it like this:

    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 deque as sequence, you can do so:

    typedef basic_seqbuf< char, char_traits<char>, std::deque<char> > dseq_buf;
    

    Or something similar... Well i haven't tested it. But maybe that's also a good thing - so if it contains still bugs, you can try fixing them.

    0 讨论(0)
  • 2021-01-05 09:04

    Use std::stringstream

    #include <iostream>
    #include <sstream>
    int main()
    {
        std::stringstream   s;
        s << "Plop" << 5;
    
        std::cout << s.str();
    }
    
    0 讨论(0)
  • 2021-01-05 09:04

    Without more detail on what you want to do it is hard to be too specific, but you don't want to create a new ostream. What you want to do is create a new type streambuf, and use an existing ostream.

    The easyist thing to do is to inherit from std::basic_filebuf<>, and overload the sync() and overflow() methods to add elements to your data structures.

    0 讨论(0)
  • 2021-01-05 09:09

    I'll just remark that you don't have to write an ostream-like class for that. You can use an adapter to achieve your goal.

    For example, this code reads from istream and inserts each element into a vector:

    vector<string> V;
    copy(istream_iterator<string>(cin), 
         istream_iterator<string>(), 
         back_inserter(V)); 
    
    0 讨论(0)
提交回复
热议问题