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

    <bdo id='KRMyX'></bdo><ul id='KRMyX'></ul>
  • <small id='KRMyX'></small><noframes id='KRMyX'>

    <legend id='KRMyX'><style id='KRMyX'><dir id='KRMyX'><q id='KRMyX'></q></dir></style></legend>
      <tfoot id='KRMyX'></tfoot>

      1. C++11 中的值初始化对象和 std::vector 构造函数

        时间:2023-10-07
          <tbody id='1tprS'></tbody>

          <small id='1tprS'></small><noframes id='1tprS'>

          <tfoot id='1tprS'></tfoot>

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

                  问题描述

                  在 C++ 中,使用 C 数组而不是 std::vector 几乎没有令人信服的理由.至少在 C++03 中,少数令人信服的原因之一是不可能使用向量来分配未初始化的对象数组.std::vector 的填充"构造函数是:

                  In C++ there are few compelling reasons to use a C array over std::vector. One of those few compelling reasons, at least with C++03, was the fact that it is impossible to use a vector to allocate an uninitialized array of objects. The "fill" constructor for std::vector is:

                  vector(size_type count, const T& value = T())

                  意思是...

                  int* array = new array[1000000];
                  

                  可能比:

                  std::vector<int> v(1000000);
                  

                  ...因为向量构造函数必须对整数数组进行零初始化.因此,当处理 POD 向量时,没有真正等效于 malloc;你能得到的最好的结果相当于 calloc.

                  ...since the vector constructor will have to zero-initialize the array of integers. Thus, when working with a vector of PODs, there is no real equivalent to malloc; the best you can get is an equivalent to calloc.

                  C++11 似乎改变了这一点,有了值初始化"的概念.在 C++11 中,std::vector 有一个新的构造函数,它接受一个 size_type 值,没有默认参数.此值初始化"向量中的所有元素.C++11 标准区分了值初始化"和零初始化".

                  C++11 seems to have changed this, with the concept of "value-initialization." In C++11, std::vector has a new constructor which takes a single size_type value, with no default argument. This "value-initializes" all elements in the vector. The C++11 standard distinguishes between "value-initialization" and "zero-initialization."

                  我的理解是值初始化"相当于在T上调用默认构造函数.如果 T 是类似于 int 的 POD 类型,那么默认构造函数只是创建一个未初始化的整数.因此,在 C++11 中,如果 T 是 POD,explicit vector::vector(size_type count) 真正等效于 malloc.

                  My understanding is that "value-initialization" is equivalent to calling the default constructor on T. If T is a POD type like int, then the default constructor simply creates an uninitialized integer. Thus, in C++11, explicit vector::vector(size_type count) is truly equivalent to malloc if T is a POD.

                  然而,我对此的理解是基于草案 C++11标准,而不是最终标准.

                  However, my understanding of this is based on the draft C++11 standard, rather than the final standard.

                  问题:我的理解是否正确?如果 T 是 POD,explicit vector::vector(size_type count) 是否提供未初始化的数组(类似于 malloc)?

                  Question: Is my understanding correct here? Does explicit vector::vector(size_type count) provide an uninitialized array (similar to malloc) if T is a POD?

                  推荐答案

                  问题:我的理解是否正确?explicit vector::vector(size_type count) 是否提供未初始化的数组(类似于malloc)如果T 是一个POD?

                  Question: Is my understanding correct here? Does explicit vector::vector(size_type count) provide an uninitialized array (similar to malloc) if T is a POD?

                  没有.C++03 和 C++11 之间存在差异,但并非如此.不同的是,在C++03中,vector(N)会默认构造一个T,然后制作N个副本它来填充向量.

                  No. There is a difference here between C++03 and C++11, but that isn't it. The difference is that in C++03, vector<T>(N) would default construct a T, and then make N copies of it to populate the vector.

                  而在 C++11 中,vector(N) 将默认构造 T N 次来填充向量.对于 POD 类型,效果是相同的.事实上,我希望几乎所有类型的效果都是相同的.然而,对于像 unique_ptr(一种仅移动类型)这样的东西,差异是至关重要的.C++03 语义永远不会工作,因为您不能复制仅移动类型.

                  Whereas in C++11, vector<T>(N) will populate the vector by default constructing T N times. For POD types the effect is identical. Indeed, I would expect that for almost all types the effect is identical. However for something like a unique_ptr (a move-only type), the difference is critical. The C++03 semantics would never work since you can not make a copy of a move-only type.

                  所以:

                  vector<unique_ptr<int>> v(10);
                  

                  创建一个包含 10 个空的 unique_ptrs(它们不是彼此的副本)的向量.

                  creates a vector of 10 null unique_ptrs (which are not copies of each other).

                  在极少数情况下,它会产生影响并且您需要可以轻松实现的 C++03 行为:

                  In the rare case that it makes a difference and you need the C++03 behavior that can easily be accomplished with:

                  vector<T> v(10, T());
                  

                  这篇关于C++11 中的值初始化对象和 std::vector 构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 istream_iterators 构造向量 下一篇:我应该总是在函数末尾调用 vector clear() 吗?

                  相关文章

                  最新文章

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

                      <tfoot id='XeRrd'></tfoot>
                    1. <small id='XeRrd'></small><noframes id='XeRrd'>

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