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

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

    <tfoot id='HDY2a'></tfoot>

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

      1. 将文件读入 std::vector<char> 的有效方法?

        时间:2023-09-16

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

          <tfoot id='htNU3'></tfoot>
              <tbody id='htNU3'></tbody>
            <legend id='htNU3'><style id='htNU3'><dir id='htNU3'><q id='htNU3'></q></dir></style></legend>

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

                • <bdo id='htNU3'></bdo><ul id='htNU3'></ul>
                • 本文介绍了将文件读入 std::vector<char> 的有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想避免不必要的副本.我的目标是:

                  I'd like to avoid unnecessary copies. I'm aiming for something along the lines of:

                  std::ifstream testFile( "testfile", "rb" );
                  std::vector<char> fileContents;
                  int fileSize = getFileSize( testFile );
                  fileContents.reserve( fileSize );
                  testFile.read( &fileContents[0], fileSize );
                  

                  (这不起作用,因为 reserve 实际上并没有在向量中插入任何东西,所以我无法访问 [0]).

                  (which doesn't work because reserve doesn't actually insert anything into the vector, so I can't access [0]).

                  当然,std::vectorfileContents(fileSize) 有效,但初始化所有元素会产生开销(fileSize 可能相当大).resize() 也一样.

                  Of course, std::vector<char> fileContents(fileSize) works, but there is an overhead of initializing all elements (fileSize can be rather big). Same for resize().

                  这个问题与开销的重要性无关.相反,我只是想知道是否还有其他方法.

                  This question is not so much about how important that overhead would be. Rather, I'm just curious to know if there's another way.

                  推荐答案

                  规范形式是这样的:

                  #include<iterator>
                  // ...
                  
                  std::ifstream testFile("testfile", std::ios::binary);
                  std::vector<char> fileContents((std::istreambuf_iterator<char>(testFile)),
                                                 std::istreambuf_iterator<char>());
                  

                  如果您担心重新分配,请在向量中保留空间:

                  If you are worried about reallocations then reserve space in the vector:

                  #include<iterator>
                  // ...
                  
                  std::ifstream testFile("testfile", std::ios::binary);
                  std::vector<char> fileContents;
                  fileContents.reserve(fileSize);
                  fileContents.assign(std::istreambuf_iterator<char>(testFile),
                                      std::istreambuf_iterator<char>());
                  

                  这篇关于将文件读入 std::vector<char> 的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何获取指向原始数据的 std::vector 指针? 下一篇:c ++ 矢量大小.为什么 -1 大于零

                  相关文章

                  最新文章

                  <tfoot id='5Rek2'></tfoot>

                  <small id='5Rek2'></small><noframes id='5Rek2'>

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