-3

I started working with the C++ STL and I am learnimg about list.

I know that in list<int> adj, adj is an object of list class in which int are stored.

My doubt is, what is mean by *adj in list<int> *adj;?

Evg
  • 25,259
  • 5
  • 41
  • 83
shubhamjr
  • 129
  • 9

1 Answers1

2

This declares adj to be a pointer to a list<int>:

list<int> *adj;

It does not construct a list<int> but can be used to point at list<int>s constructed elsewhere in the program. Example:

list<int> a_list_int_instance;

adj = &a_list_int_instance; // & is the "address-of" operator in this context

adj now points at a_list_int_instance and can access its methods by dereferencing. Here are two options:

adj->push_back(10);
(*adj).push_back(20);
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108