shuttle87
shuttle87

Reputation: 15934

Initialization list std::pair

Is there a way of initializing a std::vector of std::pair<int,int> in the initialization list in a constructor? I have a std::vector<std::pair<int,int> > and I want to initialize a certain number of pairs to (0, 0). For example, I may want to initialize 3 pairs of (0, 0) for a member in a class. How would I go about doing this?

Upvotes: 2

Views: 4612

Answers (1)

templatetypedef
templatetypedef

Reputation: 372784

You can do this by using the std::vector constructor which takes in a size and a default value to use:

class MyClass {
public:
    MyClass();
    /* ... */

private:
    std::vector<pair<int, int> > elems;
};

MyClass::MyClass() : elems(3, std::make_pair(0, 0)) {
    /* ... */
}

Upvotes: 8

Related Questions