。 但是,当我用vector替换set,因此使用push_back函数代替insert函数时,一切正常。任何人都可以解释我做错了什么? 谢谢你的建议。问题与C++当我尝试编译下面的代码设置容器
0
A
回答
3
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;
}
请记住,任何低于你使用的功能,该功能必须定义您的类型strict weak ordering。
+0
非常感谢您的明确解释。 – 2013-02-09 21:54:25
2
为了在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==
哈希值。
相关问题
- 1. 尝试编译C代码PHP
- 2. 当我尝试编译
- 3. 尝试设置iPhone-gcc编译器
- 4. 问题的编译C代码
- 5. 用C++编译器编译c代码
- 6. 下面的代码有什么问题?这不是编译
- 7. 如何C编译器解释下面的代码序列
- 8. opengl代码编译问题
- 9. 尝试当我尝试用下面的代码显示越南语字符显示与PHP
- 10. xmonad设置编译问题
- 11. C#编写代码编辑器问题
- 12. 编译电报源代码 - 面向NDK建设问题
- 13. C :: B编译器配置问题
- 14. C++编译器问题与rasterbar libtorrent API
- 15. 代码块:叮当默认编译器设置
- 16. 问题尝试编译的Hello World与Eclipse
- 17. 下面输入的代码不编译
- 18. 下面的代码如何在C/C++中编译?
- 19. C编译器问题?
- 20. C++编译器问题
- 21. 尝试编译C++代码时出现模糊错误
- 22. 当我尝试编译时出现C#错误
- 23. 尝试将代码加载到我的ios设备时调试问题
- 24. Python的 - 当我尝试运行下面的代码ElementTree-不能元素
- 25. 当我尝试编译XPCOM代码时,为什么会出现错误C2440?
- 26. Y86汇编代码编译问题
- 27. 为什么编译器不给下面的代码编译器错误?
- 28. 如何解决这个问题? C++编译器设置
- 29. 尝试编码,但有什么问题
- 30. 与JPEG PyQt4的问题,当编译
如果你不关心排序,你可能想尝试一下'std :: unordered_set',这在你看来并不在这里,根据你得到的错误来判断,我已经从编译这个错误我的脑子。 – chris 2013-02-09 03:34:28
@chris因为你必须提供一个散列和相等运算符,所以它可能会使用'std :: unordered_set'更多的工作。 – Rapptz 2013-02-09 03:44:08
@Rapptz,确实,尽管订购在我看来并不像它在这里很突出。 – chris 2013-02-09 03:46:33