C++ 向量的下界<;MyClass*>;
我有一个简单的课程:C++ 向量的下界<;MyClass*>;,c++,vector,lower-bound,C++,Vector,Lower Bound,我有一个简单的课程: class MyClass { public: int id; string name; }; 我想要一个带有指向此类对象指针的向量,该类对象按引用的MyClassid排序。我认为使用下限会很容易,以前我是用对象向量(而不是指针)来做的。对于对象,I重载操作符有两个重载: template ForwardIt下限(ForwardIt first,ForwardIt last,const T&value); 模板 ForwardIt下限(ForwardIt
class MyClass {
public:
int id;
string name;
};
我想要一个带有指向此类对象指针的向量,该类对象按引用的
MyClass
id
排序。我认为使用下限
会很容易,以前我是用对象向量(而不是指针)来做的。对于对象,I重载操作符有两个重载:
template
ForwardIt下限(ForwardIt first,ForwardIt last,const T&value);
模板
ForwardIt下限(ForwardIt first、ForwardIt last、常数T和值、比较补偿);
第一个是用于向量的,它使用运算符+1指出比较函数不需要两个参数具有相同的类型。
bool operator<(MyClass left, int right) {
return (left.id < right);
}
vector<MyClass>::iterator low;
low = lower_bound(vectorname.begin(),vectorname.end(),id);
prP = idContainer.begin();
prP = idContainer.insert(low, newobject);
template< class ForwardIt, class T >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );
template< class ForwardIt, class T, class Compare >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
std::vector<MyClass*> pvec;
auto low = std::lower_bound(pvec.begin(), pvec.end(), id,
[](const MyClass* c, const MyClass& id) {
return *c < id;
});