-1

I'm solving the Clone Graph in Leetcode, but I encountered a problem inside the following codes

 class Node {
public:
    int val;
    vector<Node*> neighbors;
    Node() {
        val = 0;
        neighbors = vector<Node*>();
    }
    Node(int _val) {
        val = _val;
        neighbors = vector<Node*>();
    }
    Node(int _val, vector<Node*> _neighbors) {
        val = _val;
        neighbors = _neighbors;
    }
};

what does it mean by the statement neighbors = vector<Node*>();. More specifically, vector<Node*>(). Why is it followed by parentheses?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
o o
  • 145
  • 6
  • You should probably learn about [objects](https://www.w3schools.com/cpp/cpp_classes.asp), [pointers](https://www.w3schools.com/cpp/cpp_pointers.asp) and [vectors](https://www.geeksforgeeks.org/vector-in-cpp-stl/) – Marko Borković Aug 11 '21 at 10:51
  • 1
    What would `Node a = Node()` do? – Lala5th Aug 11 '21 at 10:51
  • 3
    Sites such as LeetCode assume you know the computer language you will be using to solve the problem. If you are asking basic questions about C++ such as this, maybe you chose the wrong language to try and solve the LeetCode problem. LeetCode and other such sites are not in the business of teaching you C++. – PaulMcKenzie Aug 11 '21 at 10:51
  • Get your favorite book about `c++` and look up constructors. – Mestkon Aug 11 '21 at 10:52
  • 1
    It doesn't do anything here because `neighbors` was already initialized with its default constructor. – interjay Aug 11 '21 at 10:53

1 Answers1

2

Actually this statement in the constructors of the class Node

neighbors = vector<Node*>();

is redundant.

There is used the move assignment operator that assigns an empty vector created by calling the default constructor vector<Node*>() of the class std::vector<Node *> to the already created empty vector neighbors that is a data member of the class Node.

You may remove this statement from the constructors of the class Node. In fact it has no effect.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335