如何将 std::find 与不是 std::less 的 < 运算符一起使用
how to use std::find with an < operator that is not std::less
说我有
class newVector: public std::vector<T> {
public:
bool operator< (const newVector& v) {
//..
}
};
和
a std::set<newVector>;
我无法正确使用 a.find(...),我不确定要在 (...) 中放入什么才能使用 newVector::operator<。当我只放置 a.find(element) 时,它使用 std::less。我应该以某种方式更改 std::less 吗?
暂时忽略从std::vector
导出是个坏主意,我可以想到以下方法来解决这个问题:
为 newVector
的对象定义 operator<
。
class newVector: public std::vector<T> {
public:
bool operator< (const newVector& v) const {
//..
}
和
std::set<newVector> a;
a.find(...);
定义一个具有适当 operator()
函数的仿函数并使用它来创建 std::set
.
template <typename T>
struct NewVectorLess
{
bool operator()(newVector<T> const& lhs, newVector<T> const& rhs)
{
// ...
}
};
和
std::set<newVector<int>, NewVectorLess<int>> a;
a.find(...);
您不需要重载矢量,或更改 std::less,而是单独定义您自己的 std::less compatible function object。
#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct OppositeVectorComp
{
template< class T, class Alloc >
bool operator()( const std::vector<T,Alloc>& lhs,const std::vector<T,Alloc>& rhs )
{
return !(lhs < rhs);
}
};
int main() {
std::vector<int> a , b;
std::set<std::vector<int>> defaultset;
std::set<std::vector<int>, OppositeVectorComp> myset;
a.push_back(1);
b.push_back(2);
myset.insert(a);
myset.insert(b);
defaultset.insert(a);
defaultset.insert(b);
std::cout << (*myset.begin())[0] << std::endl; // output 2
std::cout << (*defaultset.begin())[0] << std::endl; // output 1
return 0;
}
此处 OppositeVectorComp
定义向量的新顺序,其中
OppositeVectorComp(a,b) true iff a <b is false
通过使用类型 std::set<std::vector<int>, OppositeVectorComp>
,我们定义了一个使用自定义 std::less 的集合。
说我有
class newVector: public std::vector<T> {
public:
bool operator< (const newVector& v) {
//..
}
};
和
a std::set<newVector>;
我无法正确使用 a.find(...),我不确定要在 (...) 中放入什么才能使用 newVector::operator<。当我只放置 a.find(element) 时,它使用 std::less。我应该以某种方式更改 std::less 吗?
暂时忽略从std::vector
导出是个坏主意,我可以想到以下方法来解决这个问题:
为
newVector
的对象定义operator<
。class newVector: public std::vector<T> { public: bool operator< (const newVector& v) const { //.. }
和
std::set<newVector> a; a.find(...);
定义一个具有适当
operator()
函数的仿函数并使用它来创建std::set
.template <typename T> struct NewVectorLess { bool operator()(newVector<T> const& lhs, newVector<T> const& rhs) { // ... } };
和
std::set<newVector<int>, NewVectorLess<int>> a; a.find(...);
您不需要重载矢量,或更改 std::less,而是单独定义您自己的 std::less compatible function object。
#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct OppositeVectorComp
{
template< class T, class Alloc >
bool operator()( const std::vector<T,Alloc>& lhs,const std::vector<T,Alloc>& rhs )
{
return !(lhs < rhs);
}
};
int main() {
std::vector<int> a , b;
std::set<std::vector<int>> defaultset;
std::set<std::vector<int>, OppositeVectorComp> myset;
a.push_back(1);
b.push_back(2);
myset.insert(a);
myset.insert(b);
defaultset.insert(a);
defaultset.insert(b);
std::cout << (*myset.begin())[0] << std::endl; // output 2
std::cout << (*defaultset.begin())[0] << std::endl; // output 1
return 0;
}
此处 OppositeVectorComp
定义向量的新顺序,其中
OppositeVectorComp(a,b) true iff a <b is false
通过使用类型 std::set<std::vector<int>, OppositeVectorComp>
,我们定义了一个使用自定义 std::less 的集合。