<tfoot id='vhK6G'></tfoot>

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

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

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

      1. 我应该总是在函数末尾调用 vector clear() 吗?

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

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

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

                • <legend id='C84IU'><style id='C84IU'><dir id='C84IU'><q id='C84IU'></q></dir></style></legend>
                • 本文介绍了我应该总是在函数末尾调用 vector clear() 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一些使用像这样的向量的简单函数(伪代码):

                  I have some simple function that uses vector like this (pseudo code):

                  void someFunc(void) {
                  
                      std::vector<std::string> contentVector;
                  
                      // here are some operations on the vector
                  
                      // should I call the clear() here or this could be ommited ?
                      contentVector.clear();
                  
                  }
                  

                  我应该调用 clear() 还是可以省略?

                  Should I call the clear() or this could be ommited ?

                  推荐答案

                  如果我们看看 cppreference.com std::vector::~vector 的条目说:

                  If we look at the cppreference.com entry for std::vector::~vector it says:

                  破坏容器.调用元素的析构函数并释放已使用的存储空间.请注意,如果元素是指针,则指向的对象不会被销毁.

                  Destructs the container. The destructors of the elements are called and the used storage is deallocated. Note, that if the elements are pointers, the pointed-to objects are not destroyed.

                  所以不,您不必调用clear.

                  如果我们要查看草案标准,我们要看看23.2.1一般容器要求4段,它说:

                  If we want to go to the draft standard, we have to look at section 23.2.1 General container requirements paragraph 4 which says:

                  在表 96 和 97 中,X 表示包含 T 类型对象的容器类,a 和 b 表示 X 类型的值,[...]

                  In Tables 96 and 97, X denotes a container class containing objects of type T, a and b denote values of type X,[...]

                  然后查看具有以下表达式条目的表 96 — 容器要求:

                  and then look at Table 96 — Container requirements which has the following expression entry:

                  (&a)->~X()  
                  

                  以及以下注意事项:

                  注意:析构函数应用于a的每个元素;所有内存都被释放.

                  note: the destructor is applied to every element of a; all the memory is deallocated.

                  更新

                  这是 RAII 的实际操作,正如 Bjarne Stroustrup 在为什么 C++ 不提供finally"构造?:

                  This is RAII in action and as Bjarne Stroustrup says in Why doesn't C++ provide a "finally" construct?:

                  因为 C++ 支持一种几乎总是更好的替代方法:资源获取即初始化"技术(TC++PL3 第 14.4 节).基本思想是用一个本地对象来表示一个资源,这样本地对象的析构函数就会释放该资源.这样程序员就不会忘记释放资源了.

                  Because C++ supports an alternative that is almost always better: The "resource acquisition is initialization" technique (TC++PL3 section 14.4). The basic idea is to represent a resource by a local object, so that the local object's destructor will release the resource. That way, the programmer cannot forget to release the resource.

                  这篇关于我应该总是在函数末尾调用 vector clear() 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++11 中的值初始化对象和 std::vector 构造函数 下一篇:我如何定义双括号/双迭代器运算符,类似于向量

                  相关文章

                  最新文章

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

                  <tfoot id='Swi7k'></tfoot>
                • <legend id='Swi7k'><style id='Swi7k'><dir id='Swi7k'><q id='Swi7k'></q></dir></style></legend>

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

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