c++设置容器的问题
problems with c++ set container
当我尝试编译以下代码时:
#include <iostream>
#include <set>
#include <vector>
using namespace std;
template <class T, class S>
class Property
{
public:
pair<T,S> p;
Property(T t, S s) { p = make_pair(t,s);}
};
int main()
{
set< Property<string, string> > properties;
Property<string, string> name("name", "Andy");
properties.insert(name);
}
我得到了编译错误。然而,当我用向量替换集合,因此使用push_back函数而不是insert函数时,一切都很好。有人能解释一下我做错了什么吗?谢谢你的建议。
std::set
将其值存储在一个排序的二进制树中,因此它需要知道如何比较它所持有的值。默认情况下,它使用std::less
作为比较函数,对于非专用的用户定义类型,它尝试调用operator<
。因此,告诉集合如何比较对象的最简单方法是为类定义一个operator<
:
template <class T, class S>
class Property
{
public:
pair<T,S> p;
Property(T t, S s) { p = make_pair(t,s);}
bool operator<(const Property<T,S>& rhs) const
{
return p < rhs.p;
}
};
然而,还有其他方法可以告诉std::set
如何比较您的类型。一种是为您的类专门化std::less
模板:
namespace std {
template<typename T,typename S>
struct less<Property<T, S> >
{
bool operator()(const Property<T, S>& lhs, const Property<T,S>& rhs) const
{
return lhs.p < rhs.p;
}
};
}
另一种方法是用具有正确签名的函数或具有使用正确签名定义的operator()
的类来替换默认比较类型。这就是事情开始变得丑陋的地方。
// Comparison function
template<typename T, typename S>
bool property_less_function(const Property<T,S>& lhs, const Property<T,S>& rhs)
{
return lhs.p < rhs.p;
}
// Comparison functor
template<typename T, typename S>
struct PropertyLess
{
bool operator()(const Property<T,S>& lhs, const Property<T,S>& rhs) const
{
return lhs.p < rhs.p;
}
};
int main()
{
// Set using comparison function.
// Have to pass the function pointer in the constructor so it knows
// which function to call. The syntax could be cleaned up with some
// typedefs.
std::set<Property<std::string, std::string>,
bool(*)(const Property<std::string, std::string>&,
const Property<std::string, std::string>&)>
set1(&property_less_function<std::string, std::string>);
// Set using comparison functor. Don't have to pass a value for the functor
// because it will be default constructed.
std::set<Property<std::string, std::string>, PropertyLess<std::string, std::string> > set2;
}
请记住,无论您使用的函数是什么,该函数都必须为您的类型定义严格的弱排序。
为了在std::set
中插入一些内容,您需要定义operator<
。
例如,这在GCC 4.7.2:上编译良好
#include <iostream>
#include <set>
#include <vector>
using namespace std;
template <class T, class S>
class Property
{
public:
pair<T,S> p;
Property(T t, S s) {
p = make_pair(t,s);
}
bool operator<(const Property& p2) const {
//Something naive..
return p < p2.p;
}
};
int main()
{
set< Property<string, string> > properties;
Property<string, string> name("name", "Andy");
properties.insert(name);
}
另一种选择是使用std::unordered_set
,尽管这需要为密钥提供哈希并定义operator==
。
相关文章:
- 将十六进制值设置为用于填充的字符变量时出现问题
- std::p 设置或多组的修订问题
- 在Visual "Microsoft studio 2019"上设置OpenCV 4.1.1时遇到问题?(非法指令。
- 嵌套的类成员变量不可访问.链表设置.V、下一个和新的问题
- C / C++链接问题与非常简单的设置
- 通过比较字符设置字符串时出现问题
- 在 c++ 中为我的类设置/获取方法时遇到问题
- 在设置 if-else 语句以删除特定货币并保持更新时遇到问题
- Coin-CLP设置的问题
- C 无序的设置问题与结构哈希
- 为 C++ 开发设置 GTK+ 时遇到问题
- 可视C++ 设置串行 COM 端口时出现问题
- 在将结构插入设置C++时遇到问题
- 使用设置精度在C++中舍入小数点时出现问题
- 未在另一个函数中设置变量引用的问题
- 设置Ncurses和C++时出现问题
- C awesomium设置了听众问题
- 为英特尔C++编辑器"GCC not found"设置环境变量时出现问题
- 问题:设置IP
- Eclipse c++ IDE,问题设置