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

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

        <tfoot id='PLrpG'></tfoot>

        标准对矢量调用 clear 如何改变容量有什么说法

        时间:2023-09-16
          1. <i id='6N46C'><tr id='6N46C'><dt id='6N46C'><q id='6N46C'><span id='6N46C'><b id='6N46C'><form id='6N46C'><ins id='6N46C'></ins><ul id='6N46C'></ul><sub id='6N46C'></sub></form><legend id='6N46C'></legend><bdo id='6N46C'><pre id='6N46C'><center id='6N46C'></center></pre></bdo></b><th id='6N46C'></th></span></q></dt></tr></i><div id='6N46C'><tfoot id='6N46C'></tfoot><dl id='6N46C'><fieldset id='6N46C'></fieldset></dl></div>
            <legend id='6N46C'><style id='6N46C'><dir id='6N46C'><q id='6N46C'></q></dir></style></legend>

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

                  <bdo id='6N46C'></bdo><ul id='6N46C'></ul>
                • <tfoot id='6N46C'></tfoot>

                    <tbody id='6N46C'></tbody>
                  本文介绍了标准对矢量调用 clear 如何改变容量有什么说法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  该网站暗示清除矢量可能会改变容量:

                  This website implies that clearing a vector MAY change the capacity:

                  http://en.cppreference.com/w/cpp/container/矢量/清除

                  很多实现不会在调用后释放分配的内存到clear(),有效地留下了vector的capacity()不变.

                  Many implementations will not release allocated memory after a call to clear(), effectively leaving the capacity() of the vector unchanged.

                  但根据@JamesKanze 的说法,这是错误的,清除的标准指令不会改变容量.

                  But according to @JamesKanze this is wrong and the standard mandates that clear will not change capacity.

                  标准怎么说?

                  推荐答案

                  取决于您正在查看的标准版本,clear 定义为等价于 erase(begin(), end()),或(在 C++11 中):
                  "销毁a中的所有元素.使所有元素失效引用、指针和迭代器a 和的元素可能会使结束迭代器."

                  Depending on the version of the standard you are looking at, clear is defined as the equivalent of erase(begin(), end()), or (in C++11):
                  "Destroys all elements in a. Invalidates all references, pointers, and iterators referring to the elements of a and may invalidate the past-the-end iterator."

                  在任何情况下都不允许修改容量;以下代码由安全保证标准:

                  In neither case is it allowed to modify the capacity; the following code is guaranteed safe by the standard:

                  std::vector<int> v;
                  for (int i = 0; i != 5; ++ i) {
                      v.push_back(i);
                  }
                  assert(v.capacity() >= 5);
                  v.clear();
                  assert(v.capacity() >= 5);
                  v.push_back(10);
                  v.push_back(11);
                  std::vector<int>::iterator i = v.begin() + 1;
                  v.push_back(12);
                  v.push_back(13);
                  *i = 42;        //  i must still be valid, because none of 
                                  //  the push_back would have required an
                                  //  increase of capacity
                  

                  (C++11措辞变化的原因:委员会不想为 clear 要求 MoveAssignable,这会如果它是根据 erase 定义的,情况就是如此.)

                  (The reason for the change in wording in C++11: the committee didn't want to require MoveAssignable for clear, which would have been the case if it were defined in terms of erase.)

                  这篇关于标准对矢量调用 clear 如何改变容量有什么说法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何缩小 std::vector 的大小? 下一篇:为什么可以从函数返回“向量"?

                  相关文章

                  最新文章

                    <tfoot id='wO0gP'></tfoot>

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

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

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