是否可以将一个带参数的构造函数传递到C++中的集合中?
我想知道是否有可能传递一个构造函数,将一个参数带到一个比较函数的集合中 例如,类似这样的事情:是否可以将一个带参数的构造函数传递到C++中的集合中?,c++,string,multiset,C++,String,Multiset,我想知道是否有可能传递一个构造函数,将一个参数带到一个比较函数的集合中 例如,类似这样的事情: 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
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?
}
std::set和std::multiset都有构造函数,它们接受comparator对象:
explicit set (const Compare& comp = Compare(),
const Allocator& = Allocator());
比较对象的类型是std::set或std::multiset模板的第二个参数。std::set和std::multiset具有构造函数,这些构造函数接受比较器对象:
explicit set (const Compare& comp = Compare(),
const Allocator& = Allocator());
比较对象的类型是std::set或std::multiset模板的第二个参数。因此它应该是multiset mscmps;。哦,这很管用,但是我可以用它来插入multisetmscmps;。哦,这很管用,但是我可以用它插入到多重集合中吗