-2

I am trying to declare the adjacency list using list std with the following line.

list<Node> *adjList;

In the constructor, I found out that these two initialization is possible.

adjList = new list<Node>[V];

adjList = new list<Node>(V);

where V is the total number of Vertex in the Graph.

My question is

What is the difference between [V] and (V)

srbemr
  • 318
  • 1
  • 2
  • 8
  • [V] callls the new[] operator and is used for arrays. (V) calls the constructor. – tinkerbeast Nov 08 '19 at 05:14
  • Side note: a pointer to a `list`, or any other library container` is usually a bad idea. Library containers exist to manage resources for you and by dynamically allocating one you're taking some of those responsibilities back onto yourself. Since resource management is one of the trickier things to get right in programming, the more you can get experts to do for you, the better. Prefer `list adjList;`, but if you find a case where you must dynamically allocate, guard the allocation with a [smart pointer](https://en.cppreference.com/w/cpp/memory). – user4581301 Nov 08 '19 at 05:16
  • @Emrah Sariboz I didn't personally downvote, but the official stackoverflow reason would be "This question does not show any research effort" – tinkerbeast Nov 08 '19 at 05:17
  • Downviote rational: No research effort. This is covered very early on in any non-fraudulent programming text. – user4581301 Nov 08 '19 at 05:17
  • To be honest, I tried to search. I just couldn't get the correct query for this particular problem – srbemr Nov 08 '19 at 05:19
  • Programming syntax is hard to search for online. Most search engines don't handle the characters used particularly well. Worse, until you know the language well enough to inoculate yourself from the bad information that proliferates on the Internet, how will you know if you are getting correct information? [Get a trusted book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and use it as your starting point. – user4581301 Nov 08 '19 at 05:23

1 Answers1

0

new list<Node>[V]

Creates an array of V number of list<Node> objects that are all initially empty.

new list<Node>(V)

Creates a single list<Node> object containing V number of Node elements.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770