0

I couldn't understand the syntax of the way the vector root has been initialized with size sz in the constructor - UnionFind(int sz) : root(sz)

class UnionFind {
    public:
        UnionFind(int sz) : root(sz) {
            for (int i = 0; i < sz; i++) {
                root[i] = i;
            }
        }
    private:
        vector<int> root;
    };
    
    
    int main() {
        
       
        UnionFind uf(10);
    }
  • 1
    [Its called a "member initializer list"](https://en.cppreference.com/w/cpp/language/constructor) – Avi Berger Sep 01 '22 at 03:03
  • This is explained in any beginner level [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and various SO posts. Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of SO related question for this. – Jason Sep 01 '22 at 03:05
  • 1
    Somyadeep, I added a thorough answer to your specific question here: https://stackoverflow.com/a/73564212/4561887 – Gabriel Staples Sep 01 '22 at 04:09

1 Answers1

0

In any class, the members of class can be used like above the code type. Because constructor can access the members using : operator.

Sniper Dev
  • 44
  • 4