<tfoot id='BGmhJ'></tfoot>
<legend id='BGmhJ'><style id='BGmhJ'><dir id='BGmhJ'><q id='BGmhJ'></q></dir></style></legend>
      • <bdo id='BGmhJ'></bdo><ul id='BGmhJ'></ul>

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

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

      1. 如果向量为空, std::vector::data() 应该返回什么?

        时间:2023-09-15
      2. <legend id='hWMTV'><style id='hWMTV'><dir id='hWMTV'><q id='hWMTV'></q></dir></style></legend>
      3. <small id='hWMTV'></small><noframes id='hWMTV'>

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

              <tfoot id='hWMTV'></tfoot>
                  <tbody id='hWMTV'></tbody>
                  <bdo id='hWMTV'></bdo><ul id='hWMTV'></ul>

                  本文介绍了如果向量为空, std::vector::data() 应该返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  根据标准草案(23.3.6.4 向量数据)、data()指向底层数组且[data(), data() + size())必须是有效范围:

                  According to the draft standard (23.3.6.4 vector data), data() points to the underlying array and [data(), data() + size()) must be a valid range:

                  T* data() noexcept;
                  const T* data() const noexcept;
                  
                      1 Returns: A pointer such that [data(),data() + size()) is a valid range. For a non-empty vector,
                  data() == &front().
                      2 Complexity: Constant time
                  

                  但是如果向量是空的呢?当我构造一个零大小的向量时:

                  But what if the vector is empty? When I construct a zero-size vector:

                  #include <vector>
                  #include <iostream>
                  
                  int main() {
                      const int NUM = 0*10;
                      std::vector< double > v( NUM, 0.0 );
                      std::cerr << "V : "<< v.data() << std::endl;
                  }
                  

                  MSVC 2010 返回 null,但在 Linux(使用 GCC 4.2.1 和 Intel 12.1)上我得到一个非 null 地址.

                  MSVC 2010 returns null, but on Linux (with GCC 4.2.1 and Intel 12.1) I get a non-null address.

                  是否允许 vector::data() 或者它应该返回 null?例如,实现是否可以进行默认大小的初始分配并返回指向它的(非空)指针?

                  Is vector::data() allowed to or should it return null? Could an implementation, for example, do a default-size initial allocation and return a (non-null) pointer to it?

                  几个答案侧重于空范围的有效性.我完全同意.

                  Several answers focus on the validity of an empty-range. I fully agree there.

                  我真的很想看到一个很好的参考或解释:是否允许必须返回空值或可能也返回非空?

                  I would really like to see a good reference or explanation for: Is it allowed to, must it return null or may it also return non-null?

                  推荐答案

                  范围的约定是[inclusive,exclusive),也就是说如果你遍历一个范围 [X,Y) 您将从概念上执行以下操作(伪代码):

                  The convention for ranges is [inclusive, exclusive), that is if you iterate over a range [X,Y) you will conceptually do the following (pseudo-code):

                  for( iterator ii = X; ii != Y; ++ii) {
                  ...
                  }
                  

                  这允许将空范围表示为 [X,X).此外,这个空范围为每个地址定义得很好,无论它是有效还是无效.

                  This permits to express an empty range as [X,X). Also this empty range is perfectly well defined for each address, no matter if it is valid or invalid.

                  也就是说 data() 的要求是(强调我的):

                  That said the requirements for data() are (emphasis mine):

                  23.3.6.4 [vector.data]

                  T* data() noexcept;

                  T* data() noexcept;

                  const T* data() const noexcept;

                  const T* data() const noexcept;

                  返回:一个指针,使得[data(),data() + size()) 是一个有效范围.为一个非空向量,data() == &front().

                  Returns: A pointer such that [data(),data() + size()) is a valid range. For a non-empty vector, data() == &front().

                  在我看来,唯一的无条件保证是 [data(),data() + size()) 应该是一个有效范围.对于 size() == 0 成员函数 data() 可以返回任何值,范围将是一个有效的空范围.因此,我会说如果size()为零,则允许实现返回非空指针.

                  It seems to me that the only unconditional guarantee is that [data(),data() + size()) should be a valid range. For size() == 0 the member function data() may return any value and the range will be a valid empty range. Therefore I would say that an implementation is allowed to return a non-null pointer if size() is zero.

                  这篇关于如果向量为空, std::vector::data() 应该返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:std::vectors 的 std::vectors 连续性 下一篇:在前面插入向量

                  相关文章

                  最新文章

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

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