c++ - 是否可以在 C++ 中传递一个将参数传入集合的构造函数?

标签 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(int argc, char * argv[])
{
    string s(argv[1]);
    multiset<int, cmp(s)> ms; //can i do this?
}

最佳答案

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

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

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

关于c++ - 是否可以在 C++ 中传递一个将参数传入集合的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9559655/

相关文章:

c++ - 将一个指针分配给另一个指针时出现段错误

java - 打印给定字符串中所有可能的子字符串,不重复字符

javascript - 如何让 lodash _.replace 字符串中所有出现的地方?

string - YAML引用另一个文件中的另一个变量

algorithm - 如何找到给定集合中多重集合的所有子集?

c++ - 无法推导具有嵌套类型的模板函数

c++ - C 函数转换为 C++

c++ - 如何在多集中打印值?

prolog - 在 prolog 中为多重集编写并集运算

c++ - WinInet上传文件