user798774
user798774

Reputation: 413

Is it possible to pass a constructor that takes an argument into a set in C++?

I was wondering if it was at all possible to pass a constructor that takes an argument to a comparison function into a set.

For example something like this:

class cmp
{
    private:
        string args_;
    public:
        cmp(const string& s):args_(s){}
        bool operator()(const int & a, const int& b)
            return a<b;
}

int main(int argc, char * argv[])
{
    string s(argv[1]);
    multiset<int, cmp(s)> ms; //can i do this?
}

Upvotes: 2

Views: 99

Answers (1)

Rafał Rawicki
Rafał Rawicki

Reputation: 22690

std::set and std::multiset have constructors, which take the comparator object:

explicit set (const Compare& comp = Compare(),
              const Allocator& = Allocator());

The type of the Compare object is the second argument of the std::set or std::multiset template.

Upvotes: 4

Related Questions