我想知道是否有可能将一个将比较函数的参数传递到集合中的构造函数。
例如这样的事情:
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
有构造函数,它接受比较器对象:
explicit set (const Compare& comp = Compare(),
const Allocator& = Allocator());
Compare 对象的类型是 std::set 或 std::multiset 模板的第二个参数。
关于c++ - 是否可以在 C++ 中传递一个将参数传入集合的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9559655/