• <small id='mHaAt'></small><noframes id='mHaAt'>

        • <bdo id='mHaAt'></bdo><ul id='mHaAt'></ul>

        <tfoot id='mHaAt'></tfoot>
      1. <legend id='mHaAt'><style id='mHaAt'><dir id='mHaAt'><q id='mHaAt'></q></dir></style></legend>

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

        如何将整个流读入 std::string?

        How to read entire stream into a std::string?(如何将整个流读入 std::string?)
      2. <tfoot id='Mn0YH'></tfoot>

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

                  <small id='Mn0YH'></small><noframes id='Mn0YH'>

                • <legend id='Mn0YH'><style id='Mn0YH'><dir id='Mn0YH'><q id='Mn0YH'></q></dir></style></legend>
                  本文介绍了如何将整个流读入 std::string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将整个流(多行)读入一个字符串.

                  I'm trying to read an entire stream (multiple lines) into a string.

                  我正在使用这段代码,它可以工作,但它冒犯了我的风格……当然有更简单的方法吗?也许使用字符串流?

                  I'm using this code, and it works, but it's offending my sense of style... Surely there's an easier way? Maybe using stringstreams?

                  void Obj::loadFromStream(std::istream & stream)
                  { 
                    std::string s;
                  
                    std::streampos p = stream.tellg();  // remember where we are
                  
                    stream.seekg(0, std::ios_base::end); // go to the end
                    std::streamoff sz = stream.tellg() - p;  // work out the size
                    stream.seekg(p);        // restore the position
                  
                    s.resize(sz);          // resize the string
                    stream.read(&s[0], sz);  // and finally, read in the data.
                  

                  <小时>实际上,对字符串的 const 引用也可以,这可能会使事情变得更容易...


                  Actually, a const reference to a string would do as well, and that may make things easier...

                  const std::string &s(... a miracle occurs here...)
                  

                  推荐答案

                  怎么样

                  std::istreambuf_iterator<char> eos;
                  std::string s(std::istreambuf_iterator<char>(stream), eos);
                  

                  (如果不是 MVP 可能是单线)

                  (could be a one-liner if not for MVP)

                  2011 年之后的编辑,这种方法现在拼写

                  post-2011 edit, this approach is now spelled

                  std::string s(std::istreambuf_iterator<char>(stream), {});
                  

                  这篇关于如何将整个流读入 std::string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

                  <tfoot id='JqSOJ'></tfoot>

                  <small id='JqSOJ'></small><noframes id='JqSOJ'>

                    <legend id='JqSOJ'><style id='JqSOJ'><dir id='JqSOJ'><q id='JqSOJ'></q></dir></style></legend>

                      <tbody id='JqSOJ'></tbody>
                    • <bdo id='JqSOJ'></bdo><ul id='JqSOJ'></ul>

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