创建一个ostream

发布于 2024-07-13 06:52:09 字数 97 浏览 5 评论 0原文

出于教育原因,我正在尝试创建一个 C++ ostream。 我的测试将创建一个 ostream,其行为类似于 ofstream,只不过它不是写入文件,而是写入双端队列或向量容器。

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 write to a deque or vector container.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月无声 2024-07-20 06:52:10

正如您所说,因为这是为了教育,我将向您展示我将如何做这样的事情。 否则,stringstream确实是正确的选择。

听起来您想创建一个 Streambuf 实现,然后写入向量/双端队列。 像这样的事情(从我的另一个答案复制针对/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;

你可以像这样使用它:

seqbuf s;
std::ostream os(&s);
os << "hello, i'm " << 22 << " years old" << std::endl;
std::vector<char> v = s.get_sequence();

如果你想有一个双端队列作为序列,你可以这样做:

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

或者类似的东西......好吧我还没有测试过它。 但也许这也是一件好事 - 所以如果它仍然包含错误,您可以尝试修复它们。

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.

扎心 2024-07-20 06:52:10

使用 std::stringstream

#include <iostream>
#include <sstream>
int main()
{
    std::stringstream   s;
    s << "Plop" << 5;

    std::cout << s.str();
}

Use std::stringstream

#include <iostream>
#include <sstream>
int main()
{
    std::stringstream   s;
    s << "Plop" << 5;

    std::cout << s.str();
}
最舍不得你 2024-07-20 06:52:10

我只想指出,您不必为此编写类似 ostream 的类。 您可以使用适配器来实现您的目标。

例如,以下代码从 istream 读取并将每个元素插入到向量中:

vector<string> V;
copy(istream_iterator<string>(cin), 
     istream_iterator<string>(), 
     back_inserter(V)); 

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)); 
哽咽笑 2024-07-20 06:52:10

如果没有更多关于您想要做什么的详细信息,很难太具体,但您不想创建一个新的 ostream。 您想要做的是创建一个新类型的streambuf,并使用现有的ostream。

最简单的做法是继承 std::basic_filebuf<>,并重载sync() 和overflow() 方法以将元素添加到数据结构中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文