C++ c++;编译器可以';不要使用我的超负荷<;位集密钥比较运算符
我知道我总是可以使用集合(comp)构造函数初始化集合的比较函数C++ c++;编译器可以';不要使用我的超负荷<;位集密钥比较运算符,c++,overloading,grammar,C++,Overloading,Grammar,我知道我总是可以使用集合(comp)构造函数初始化集合的比较函数 但是我通常更喜欢重载运算符运算符必须在所讨论的类的命名空间中重载: namespace std { bool operator<(const bitset<128>& x, const bitset<128>& y) { return false;//show the brief idea of overloading operator< ,never m
但是我通常更喜欢重载运算符运算符必须在所讨论的类的命名空间中重载:
namespace std {
bool operator<(const bitset<128>& x, const bitset<128>& y)
{
return false;//show the brief idea of overloading operator< ,never mind .
};
}
using namespace std;
// The rest of the code in the question.
名称空间std{
BoOL运算符< P>这与C++中的名称查找复杂性有关。
默认情况下,std::set
使用std::less
作为其比较器,其实现类似于:
template <class K>
struct less {
bool operator()(K const& lhs, K const& rhs) const {
return lhs < rhs; // (*)
}
};
模板
无结构{
布尔运算符(){
返回lhs
标记行是std::set
代码通过参数相关查找查找查找比较函数的位置。除了禁止在名称空间std
中引入新内容之外,您可以在那里定义它,因为std::bitset
存在于此。但最好将比较器函数传递给constructor.@Cheers-Sandhth.-Alf谢谢。但不要在名称空间std
中引入新事物。你可以在那里专门化。但不是这个。
#include <bitset>
#include <set>
using namespace std;
struct wrapper
{
std::bitset<128> a;
};
bool operator<(const std::bitset<128>& x, const std::bitset<128>& y)
{
return false;
};
bool operator<(const wrapper& x, const wrapper& y)
{
return x.a<y.a;
};
int main()
{
set<wrapper> s{ wrapper() };
return 0;
}
namespace std {
bool operator<(const bitset<128>& x, const bitset<128>& y)
{
return false;//show the brief idea of overloading operator< ,never mind .
};
}
using namespace std;
// The rest of the code in the question.
template <class K>
struct less {
bool operator()(K const& lhs, K const& rhs) const {
return lhs < rhs; // (*)
}
};