• <i id='oLAcf'><tr id='oLAcf'><dt id='oLAcf'><q id='oLAcf'><span id='oLAcf'><b id='oLAcf'><form id='oLAcf'><ins id='oLAcf'></ins><ul id='oLAcf'></ul><sub id='oLAcf'></sub></form><legend id='oLAcf'></legend><bdo id='oLAcf'><pre id='oLAcf'><center id='oLAcf'></center></pre></bdo></b><th id='oLAcf'></th></span></q></dt></tr></i><div id='oLAcf'><tfoot id='oLAcf'></tfoot><dl id='oLAcf'><fieldset id='oLAcf'></fieldset></dl></div>

      <bdo id='oLAcf'></bdo><ul id='oLAcf'></ul>

  • <tfoot id='oLAcf'></tfoot>

    <legend id='oLAcf'><style id='oLAcf'><dir id='oLAcf'><q id='oLAcf'></q></dir></style></legend>
  • <small id='oLAcf'></small><noframes id='oLAcf'>

      1. 将 std::cout 重定向到自定义编写器

        时间:2024-08-14
          <tbody id='5znrE'></tbody>

          <i id='5znrE'><tr id='5znrE'><dt id='5znrE'><q id='5znrE'><span id='5znrE'><b id='5znrE'><form id='5znrE'><ins id='5znrE'></ins><ul id='5znrE'></ul><sub id='5znrE'></sub></form><legend id='5znrE'></legend><bdo id='5znrE'><pre id='5znrE'><center id='5znrE'></center></pre></bdo></b><th id='5znrE'></th></span></q></dt></tr></i><div id='5znrE'><tfoot id='5znrE'></tfoot><dl id='5znrE'><fieldset id='5znrE'></fieldset></dl></div>

            <tfoot id='5znrE'></tfoot>
          1. <small id='5znrE'></small><noframes id='5znrE'>

                <bdo id='5znrE'></bdo><ul id='5znrE'></ul>

                  <legend id='5znrE'><style id='5znrE'><dir id='5znrE'><q id='5znrE'></q></dir></style></legend>

                1. 本文介绍了将 std::cout 重定向到自定义编写器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用 Mr-Edd 的 iostreams 文章中的这个片段在某处打印 std::clog.

                  I want to use this snippet from Mr-Edd's iostreams article to print std::clog somewhere.

                  #include <iostream>
                  #include <iomanip>
                  #include <string>
                  #include <sstream>
                  
                  int main()
                  {
                      std::ostringstream oss;
                  
                      // Make clog use the buffer from oss
                      std::streambuf *former_buff =
                          std::clog.rdbuf(oss.rdbuf());
                  
                      std::clog << "This will appear in oss!" << std::flush;
                  
                      std::cout << oss.str() << '\n';
                  
                      // Give clog back its previous buffer
                      std::clog.rdbuf(former_buff);
                  
                      return 0;
                  }
                  

                  所以,在主循环中,我会做类似

                  so, in a mainloop, I will do something like

                  while (! oss.eof())
                  {
                      //add to window text somewhere
                  }
                  

                  这是 ostringstream 文档,但我无法理解最好的方法来做到这一点.我有一个显示文本的方法,我只想用 ostringstream 中的任何数据调用它.

                  Here's the ostringstream docs but I'm having trouble understanding the best way to do this. I have a method that displays the text, I just want to call it with any data in the ostringstream.

                  将发送到 std::clog 的任何内容重定向到我选择的方法的最简单/最好的方法是什么?是如上所述,并填写 while !eof 部分(不确定如何),还是有更好的方法,例如通过在某处重载一些调用我的方法的提交"运算符?我正在寻找快速和简单的方法,我真的不想开始定义接收器,例如文章中所做的使用 boost iostreams 之类的东西 - 这些东西太让我费解了.

                  What is the easiest/best way to get anything sent to std::clog redirected to a method of my choice? is it as above, and fill in the while !eof part (not sure how), or is there a better way, say by overloading some 'commit' operator somewhere that calls my method? I'm loking for quick and easy, I really don't want to start defining sinks and such with boost iostreams as the article does - that stuff is way over my head.

                  推荐答案

                  我鼓励您查看 Boost.IOStreams.它似乎非常适合您的用例,而且使用起来非常简单:

                  I encourage you to look at Boost.IOStreams. It seems to fit your use-case nicely, and using it is surprisingly simple:

                  #include <boost/iostreams/concepts.hpp> 
                  #include <boost/iostreams/stream_buffer.hpp>
                  #include <iostream>
                  
                  namespace bio = boost::iostreams;
                  
                  class MySink : public bio::sink
                  {
                  public:
                      std::streamsize write(const char* s, std::streamsize n)
                      {
                          //Do whatever you want with s
                          //...
                          return n;
                      }
                  };
                  
                  int main()
                  {
                      bio::stream_buffer<MySink> sb;
                      sb.open(MySink());
                      std::streambuf * oldbuf = std::clog.rdbuf(&sb);
                      std::clog << "hello, world" << std::endl;
                      std::clog.rdbuf(oldbuf);
                      return 0;
                  }
                  

                  这篇关于将 std::cout 重定向到自定义编写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 C++ 中读取 popen 结果 下一篇:为什么我不能用operator>>读取fstream的二进制数据?

                  相关文章

                2. <small id='JXy3g'></small><noframes id='JXy3g'>

                3. <tfoot id='JXy3g'></tfoot>
                  <legend id='JXy3g'><style id='JXy3g'><dir id='JXy3g'><q id='JXy3g'></q></dir></style></legend>

                  1. <i id='JXy3g'><tr id='JXy3g'><dt id='JXy3g'><q id='JXy3g'><span id='JXy3g'><b id='JXy3g'><form id='JXy3g'><ins id='JXy3g'></ins><ul id='JXy3g'></ul><sub id='JXy3g'></sub></form><legend id='JXy3g'></legend><bdo id='JXy3g'><pre id='JXy3g'><center id='JXy3g'></center></pre></bdo></b><th id='JXy3g'></th></span></q></dt></tr></i><div id='JXy3g'><tfoot id='JXy3g'></tfoot><dl id='JXy3g'><fieldset id='JXy3g'></fieldset></dl></div>
                      <bdo id='JXy3g'></bdo><ul id='JXy3g'></ul>