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

      <small id='6oyw2'></small><noframes id='6oyw2'>

          <bdo id='6oyw2'></bdo><ul id='6oyw2'></ul>

        调整 C++ std::vector<char> 的大小无需初始化数据

        时间:2023-10-07

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

            <tbody id='VNYSy'></tbody>

          <tfoot id='VNYSy'></tfoot>

          1. <legend id='VNYSy'><style id='VNYSy'><dir id='VNYSy'><q id='VNYSy'></q></dir></style></legend>
              <bdo id='VNYSy'></bdo><ul id='VNYSy'></ul>
                <i id='VNYSy'><tr id='VNYSy'><dt id='VNYSy'><q id='VNYSy'><span id='VNYSy'><b id='VNYSy'><form id='VNYSy'><ins id='VNYSy'></ins><ul id='VNYSy'></ul><sub id='VNYSy'></sub></form><legend id='VNYSy'></legend><bdo id='VNYSy'><pre id='VNYSy'><center id='VNYSy'></center></pre></bdo></b><th id='VNYSy'></th></span></q></dt></tr></i><div id='VNYSy'><tfoot id='VNYSy'></tfoot><dl id='VNYSy'><fieldset id='VNYSy'></fieldset></dl></div>
                  本文介绍了调整 C++ std::vector<char> 的大小无需初始化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  对于向量,可以假设元素连续存储在内存中,允许范围 [&vec[0], &vec[vec.capacity()) 用作正常数组.例如,

                  With vectors, one can assume that elements are stored contiguously in memory, allowing the range [&vec[0], &vec[vec.capacity()) to be used as a normal array. E.g.,

                  vector<char> buf;
                  buf.reserve(N);
                  int M = read(fd, &buf[0], N);
                  

                  但现在向量不知道它包含 M 字节的数据,由 read() 从外部添加.我知道 vector::resize() 设置大小,但它也清除数据,所以它不能用于更新 read() 后的大小打电话.

                  But now the vector doesn't know that it contains M bytes of data, added externally by read(). I know that vector::resize() sets the size, but it also clears the data, so it can't be used to update the size after the read() call.

                  是否有一种简单的方法可以将数据直接读入向量并在之后更新大小?是的,我知道一些明显的解决方法,例如使用小数组作为临时读取缓冲区,并使用 vector::insert() 将其附加到向量的末尾:

                  Is there a trivial way to read data directly into vectors and update the size after? Yes, I know of the obvious workarounds like using a small array as a temporary read buffer, and using vector::insert() to append that to the end of the vector:

                  char tmp[N];
                  int M = read(fd, tmp, N);
                  buf.insert(buf.end(), tmp, tmp + M)
                  

                  这行得通(这就是我今天要做的),但让我烦恼的是,如果我可以将数据直接放入其中,则不需要额外的复制操作矢量.

                  This works (and it's what I'm doing today), but it just bothers me that there is an extra copy operation there that would not be required if I could put the data directly into the vector.

                  那么,在外部添加数据时,有没有一种简单的方法可以修改矢量大小?

                  So, is there a simple way to modify the vector size when data has been added externally?

                  推荐答案

                  vector<char> buf;
                  buf.reserve(N);
                  int M = read(fd, &buf[0], N);
                  

                  此代码片段调用未定义的行为.你不能写超过 size() 元素,即使你已经预留了空间.

                  This code fragment invokes undefined behavior. You can't write beyond than size() elements, even if you have reserved the space.

                  正确的代码如下:

                  vector<char> buf;
                  buf.resize(N);
                  int M = read(fd, &buf[0], N);
                  buf.resize(M);
                  


                  PS. 您的陈述对于向量,人们可以假设元素连续存储在内存中,允许范围 [&vec[0], &vec[vec.capacity()) 用作普通数组"是不正确的.允许的范围是[&vec[0], &vec[vec.size()).


                  PS. Your statement "With vectors, one can assume that elements are stored contiguously in memory, allowing the range [&vec[0], &vec[vec.capacity()) to be used as a normal array" isn't true. The allowable range is [&vec[0], &vec[vec.size()).

                  这篇关于调整 C++ std::vector<char> 的大小无需初始化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:对于先前分配的向量,为什么 push_back 比 operato 下一篇:在成对的向量中按键查找成对

                  相关文章

                  最新文章

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

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

                    <tfoot id='Y9leH'></tfoot>

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