C ++:在多集容器中使用自己的类 [英] C++: use own class in a multiset container
问题描述
起初我是新来的,英语不是我的母语,所以对于任何语法上的错误,我深表歉意,但是我发现这个社区真的很好,所以我将尝试尽可能精确地提出我的问题.
at first I'm new here and English isn't my native language so apologize for any grammatical failures but I find this community really nice so I will try to ask my question as precise as I can.
我想将自己的类对象添加到stl容器多集中,并希望使用在类中定义的我自己的重载less运算符对它进行排序.我确实尝试了几种解决方案,但没有任何实际效果,因此希望有人能给我一些有用的提示来解决它.
I want to add my own class object into a stl container multiset and want to sort it with my own overloaded less operator defined in my class. I really tried out several solutions but nothing really worked so I hope someone can give me some useful hints to solve it.
这是我对班级定义的一般理解:
Here is my general idea of my class definition:
class object {
public:
int first;
string second;
object(int f, string s) {
first = f;
second = s;
}
bool operator<(const object &comp) {
return first < comp.first;
}
};
这是我的第一次尝试,但是没有用,所以我也尝试将重载的运算符声明为朋友方法,但是它也没有用.
This was my first try and it didn't work so I also tried out to declare the overloaded operator as a friend method but it didn't work also.
这是我主要功能的简短代码摘录:
Here is a short code extract from my main function:
includes ...
//code omitted
int main() {
multiset<object*> mmset;
mmset.insert(new object(10, "test"));
mmset.insert(new object(11, "test"));
return 0;
}
一段时间后,我开始调试代码,尝试找出问题出在哪里,然后发现以下使我有些怀疑的事情.
After a while I started to debugging my code and try to figure out where the problem is and I come across the following thing that have made me a bit suspicious.
从stl中提取代码:
// TEMPLATE STRUCT less
template<class _Ty>
struct less : public binary_function<_Ty, _Ty, bool>
{ // functor for operator<
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator< to operands
return (_Left < _Right);
}
};
我已经在此行上设置了一个断点,并观察了程序在这里做什么,我不知道为什么,但是它仅比较两个对象的地址并始终返回false.尽管该运算符存在并且_Left和_Right变量包含我对象的地址,但它从未调用过重载的less运算符.
I have set a breakpoint on this line and observed what the program is doing here and I don't know why, but it only compares the addresses from the two objects and return so always false. It never calls my overloaded less operator although the operator exists and the _Left and _Right variables contain the address to my object.
如果有人能帮助我,我将非常感激.
I would really appreciate it if someone could help me.
最好的问候
汤姆
推荐答案
您没有在multiset
中存储object
.您正在存储object*
.这些是指向object
的指针.这意味着该集合将对要插入其中的指针进行排序.
You are not storing object
s in your multiset
. You are storing object*
s. These are pointers to object
s. This means the set will order the pointers that you're inserting into it.
似乎您真的只想要一个multiset<object>
:
It seems like you really just want a multiset<object>
:
multiset<object> mmset;
mmset.emplace(10, "test");
mmset.emplace(11, "test");
现在,它将使用<
来比较object
本身.
Now it will use <
to compare the object
s themselves.
如果您确实要存储指针,则需要为multiset
提供一个自定义比较器.在C ++ 11中,您可以使用lambda轻松做到这一点:
If you really want to store pointers, you'll need to provide a custom comparator to the multiset
. In C++11, you can do this easily with a lambda:
auto f = [](int* a, int* b) { return *a < *b; };
std::multiset<int*, decltype(f)> mmset(f);
在C ++ 11之前的版本中,您可以创建一个函数对象,该对象使用与此lambda函数相同的主体来实现operator()
.
Pre-C++11, you can create a function object that implements operator()
with the same body as this lambda function.
这篇关于C ++:在多集容器中使用自己的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!