如何在 C++ 中对包含自定义对象的容器使用分区函数

Dav*_*vid 1 c++ stl c++11

我正在尝试使用 parititon() API 对存储在向量中的对象进行分区。我想调用一个函数对象将向量分成两半。以下代码给我编译错误。如何在 C++ 中做到这一点?

\n\n

partition.cpp:15:48: 错误: \xe2\x80\x98)\xe2\x80\x99 token\n auto itr = partition(v.begin(),v.end()-1, 之前预期的主表达式补偿);

\n\n
#include<iostream>\n#include<algorithm>\n#include<vector>\nusing namespace std;\nclass comp{\npublic:\n  bool operator()(const pair<int,int>& p1, const pair<int,int>& p2) {\n    return p1.first < p2.first;\n  }\n};\n\n\nmain(){\n  vector<pair<int,int> > v = { {1,2} , {3,4} , {5,6} ,{7,8} };\n  auto itr = partition(v.begin(),v.end()-1,comp);\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Sev*_*eux 5

关于什么

struct comp {
    int _median;
    comp(int median): _median(median) {
    }
    bool operator()(const pair<int,int>& p) {
        return p.first < _median;
    }
};
Run Code Online (Sandbox Code Playgroud)

进而

auto i = std::partition(v.begin(), v.end(), comp{7});
Run Code Online (Sandbox Code Playgroud)

可能需要添加默认的复制 ctr、移动 ctr、op=、dtr 和其他脚手架