-1

我有:

“对象.h”

namespace objectNS
{
    class object
    {
    public:
        int m_number;
        bool operator== (const object& object) const;
        bool operator< (const object& object) const;
    };
    class compare
    {
    public:
        bool operator ()(const object*, const object*) const;
    };
}

“对象.cpp”

#include "object.h"
typedef objectNS::object OBJECT;
bool OBJECT::operator== (const object &object) const
{
    return this->m_number == object.m_number;
}

bool OBJECT::operator< (const object &object) const
{
    return this->m_number < object.m_number;
}

bool objectNS::compare::operator() (const object* obj1, const object* obj2) const
{
    return obj1->m_number > obj2->m_number;
}

我有一些适用于对象的功能

void f(void)
{
    set<Compound_object*> detectedObjects;
    set<Compound_object*> deleteFromTrackedObjects;
    set_difference(detectedObjects.begin(), detectedObjects.end(), this->m_trackedObjects.begin(), this->m_trackedObjects.end(), inserter(addToTrackedObjects, addToTrackedObjects.end()));
}

问题描述: 我实现了operator<and ,operator==通过它来执行两个实例的比较m_number但是当集合detectedObjectstrackedObjects包含相同m_number的元素时,std::set_difference返回所有元素而不是预期的空结果集。

我什至尝试将函数对象compare作为模板参数提供给集合,但结果我得到了很多编译器错误=并且!=没有正确定义。

我想知道是什么问题?

4

1 回答 1

2

我看不出为什么要存储指向对象的指针而不是对象本身的充分理由。

要使用比较器,您需要确保为所有设置实例提供相同的比较器类型,并且set_difference

set<Compound_object*, compare> m_trackedObjects;

set<Compound_object*, compare> detectedObjects;
set<Compound_object*, compare> deleteFromTrackedObjects;
set_difference(detectedObjects.begin(), detectedObjects.end(),
    this->m_trackedObjects.begin(), this->m_trackedObjects.end(),
    inserter(addToTrackedObjects, addToTrackedObjects.end()),
    compare());
于 2012-08-24T09:27:31.040 回答