简体   繁体   English

在C ++中添加元素并清除指针向量

[英]Add elements and clear a vector of pointers in C++

I would like to add 2 elements to a vector<Node*> and then clear all the elements and release the memory. 我想向vector<Node*>添加2个元素,然后清除所有元素并释放内存。
Does this code do that in a right way? 这段代码是否以正确的方式执行此操作?

#include <stdlib.h>
#include <iostream>
#include <vector>

using namespace std;

class Node {
public:
    int value;
    // ...and some other fields and methods...
};

int main(int argc, char** argv) {
    Node* n = new Node;
    n->value = 20;
    vector<Node*> v;
    v.push_back(n);
    n = new Node;
    n->value = 52;
    v.push_back(n);
    for (vector<Node*>::iterator i = v.begin(); i != v.end(); i++) {
        cout << (*i)->value << endl;
        delete *i;
        *i = NULL;
    }
    v.clear();
    return (EXIT_SUCCESS);
}

It looks fine to me. 它看起来很好。 There are a few things that I'd change (subjectively): 我会改变一些事情(主观上):

*i = NULL;  // This is unnecessary.

Then I'd avoid reusing n (actually, I'd avoid it entirely): 然后我会避免重复使用n (实际上,我完全避免使用它):

v.push_back(new Node);
v.back()->value = 20;
v.push_back(new Node);
v.back()->value = 52;

Also, you may want to consider smart pointers to track your memory for you. 此外,您可能需要考虑智能指针来跟踪您的记忆。 See shared_ptr and ptr_vector . 请参阅shared_ptrptr_vector

That will do what you expect. 这将做你期望的。 However, clear() is totally unnecessary since the vector will be destroyed right after that when you leave the current scope (which also happens to be the end of the function and the end of the program in this case). 但是, clear()是完全没必要的,因为当你离开当前作用域之后, vector将被销毁(在这种情况下也恰好是函数的结尾和程序的结尾)。 If you were planning on keeping the vector around to do more with it, then clear() would remove all of the pointers from the vector . 如果你打算保持vector来做更多的事情,那么clear()将从vector删除所有指针。 As it is, the vector is being destroyed right after, so there's no point in calling clear() . 事实上, vector正在被摧毁,因此调用clear()没有意义。

Also, the nitpicker in me wants to say that you should use ++i in your loop instead of i++ since i++ creates a temporary that the compiler can't optimize away (since you're dealing with an overloaded operator). 另外,我的讨厌者想说你应该在你的循环而不是i++使用++i ,因为i++创建了一个临时的,编译器无法优化掉(因为你正在处理一个重载的运算符)。 Also, since you're just going to destroy the vector right after deleting everything in it, there's not much point in setting all of it's elements to NULL . 此外,由于您只是在删除其中的所有内容后立即销毁该vector ,因此将其所有元素设置为NULL并没有多大意义。 If you were going to re-use the elements rather than clear or destroy the vector , then that would be a good idea. 如果你要重新使用元素而不是清除或破坏vector ,那么这将是一个好主意。 But in this case, it's just inefficient. 但在这种情况下,它只是效率低下。

Yes that works. 是的有效。

Some remarks : 一些评论:

  • Instead of including stdlib.h, the c++ equivalent is cstdlib. 而不是包括stdlib.h,c ++等价物是cstdlib。
  • Your vector could be vector<Node> if you don't really need pointers; 如果你真的不需要指针,你的向量可以是vector<Node> ; and it would be better to use smart pointers if you need them. 如果你需要它们,最好使用智能指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM