I Couldnt understand this syntax, related to vector intialization can someone explain? [duplicate]
Asked Answered
L

1

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);
    }
Luker answered 1/9, 2022 at 2:55 Comment(3)
Its called a "member initializer list"Wakayama
This is explained in any beginner level c++ book and various SO posts. Refer to how to ask where the first step is to "search and then research" and you'll find plenty of SO related question for this.Dilapidated
Somyadeep, I added a thorough answer to your specific question here: https://mcmap.net/q/80502/-what-is-this-weird-colon-member-quot-quot-syntax-in-the-constructorBrundisium
E
0

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

Equator answered 1/9, 2022 at 3:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.