2

我想知道是否有可能将一个将参数传递给比较函数的构造函数传递给一个集合。

例如这样的:

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?
}
4

1 回答 1

4

std::setstd::multiset有构造函数,它接受比较器对象:

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

对象的类型是or模板Compare的第二个参数。std::setstd::multiset

于 2012-03-04T23:02:37.007 回答