c ++结构运算符重载不起作用
c++ struct operator overload not working
我有一个指向结构的指针向量,我想检查现有项并按结构成员的值排序。但是,似乎对现有项的检查(我正在使用 QVector::indexOf())不起作用,并且 std::sort 正在将其排序应用于某个指针值而不是成员值。我不明白为什么结构运算符重载没有被调用(或被正确调用)。
在头文件中:
struct PlotColumn {
quint32 octagonLength;
QVector<Qt::GlobalColor> columnVector;
bool operator< (const PlotColumn& pc) const
{
return (this->octagonLength < pc.octagonLength);
}
bool operator==(PlotColumn const& pc)
{
return this->octagonLength == pc.octagonLength;
}
bool operator> (const PlotColumn& pc) const
{
return (this->octagonLength > pc.octagonLength);
}
};
QVector<PlotColumn*> *plotMap;
在源文件中:
PlotColumn *pcNew = new PlotColumn();
pcNew->octagonLength = octagonLength;
// check if octagonLength has arrived before:
int plotXAxis = plotMap->indexOf(pcNew);
if (plotXAxis == -1) { // unknown, so insert:
... (some housekeeping)
plotMap->append(pcNew);
std::sort(plotMap->begin(), plotMap->end());
....
(some more housekeeping)
}
可以使用外部(不在结构中)进行排序,但不能用于 indexOf (afaik)。
有什么想法吗?
PS:是的,我知道有很多关于对指向结构的指针向量进行排序的问题,我已经尝试在这些解决方案中应用该模式,但它仍然不起作用。
您需要为 std::sort()
提供一个使用指针的比较器。
bool PlotColumnPointerComparator(const PlotColumn *a, const PlotColumn *b)
{
return (*a) < (*b); // this will call your PlotColumn:: operator<()
}
并将您的 std::sort()
语句更改为
std::sort(plotMap->begin(), plotMap->end(), PlotColumnPointerComparator);
在 C++11 中,您可以使用 lambda 函数执行上述操作,但这只是一种语法上的便利。
编译器不是万能的读心器。如果你告诉它对一组指针进行排序,它就会进行指针比较。如果您希望比较取消引用指针并比较指向的对象,则需要告诉它这样做....如上所示。
std::sort
is applying its sorting to some pointer value
让我们看看:
QVector<PlotColumn*> *plotMap
std::sort(plotMap->begin(), plotMap->end());
是的,这正是你告诉 std::sort
要做的(假设 QVector
类似于 vector
):你有一个指针容器,告诉它对容器中的元素进行排序.因此,您告诉它对指针进行排序,而不是 PlotColumn
对象。
如果您想根据指针指向的对象进行比较来对指针进行排序,则必须应用其中之一
answered questions about sorting vectors of pointers to structs
您已经确定了您的问题:
I've tried to apply the pattern in those solutions but it still doesn't work.
不要放弃那条询问线:你已经正确地确定了你需要做的事情,应该努力理解这些模式是如何工作的以及如何将它们应用到你的问题上,也许会问一个关于你在这方面的尝试的重点问题。
它将不起作用,因为您使用的是相同的签名。重载适用于不同的签名。检查您的函数签名。
我有一个指向结构的指针向量,我想检查现有项并按结构成员的值排序。但是,似乎对现有项的检查(我正在使用 QVector::indexOf())不起作用,并且 std::sort 正在将其排序应用于某个指针值而不是成员值。我不明白为什么结构运算符重载没有被调用(或被正确调用)。
在头文件中:
struct PlotColumn {
quint32 octagonLength;
QVector<Qt::GlobalColor> columnVector;
bool operator< (const PlotColumn& pc) const
{
return (this->octagonLength < pc.octagonLength);
}
bool operator==(PlotColumn const& pc)
{
return this->octagonLength == pc.octagonLength;
}
bool operator> (const PlotColumn& pc) const
{
return (this->octagonLength > pc.octagonLength);
}
};
QVector<PlotColumn*> *plotMap;
在源文件中:
PlotColumn *pcNew = new PlotColumn();
pcNew->octagonLength = octagonLength;
// check if octagonLength has arrived before:
int plotXAxis = plotMap->indexOf(pcNew);
if (plotXAxis == -1) { // unknown, so insert:
... (some housekeeping)
plotMap->append(pcNew);
std::sort(plotMap->begin(), plotMap->end());
....
(some more housekeeping)
}
可以使用外部(不在结构中)进行排序,但不能用于 indexOf (afaik)。
有什么想法吗?
PS:是的,我知道有很多关于对指向结构的指针向量进行排序的问题,我已经尝试在这些解决方案中应用该模式,但它仍然不起作用。
您需要为 std::sort()
提供一个使用指针的比较器。
bool PlotColumnPointerComparator(const PlotColumn *a, const PlotColumn *b)
{
return (*a) < (*b); // this will call your PlotColumn:: operator<()
}
并将您的 std::sort()
语句更改为
std::sort(plotMap->begin(), plotMap->end(), PlotColumnPointerComparator);
在 C++11 中,您可以使用 lambda 函数执行上述操作,但这只是一种语法上的便利。
编译器不是万能的读心器。如果你告诉它对一组指针进行排序,它就会进行指针比较。如果您希望比较取消引用指针并比较指向的对象,则需要告诉它这样做....如上所示。
std::sort
is applying its sorting to some pointer value
让我们看看:
QVector<PlotColumn*> *plotMap
std::sort(plotMap->begin(), plotMap->end());
是的,这正是你告诉 std::sort
要做的(假设 QVector
类似于 vector
):你有一个指针容器,告诉它对容器中的元素进行排序.因此,您告诉它对指针进行排序,而不是 PlotColumn
对象。
如果您想根据指针指向的对象进行比较来对指针进行排序,则必须应用其中之一
answered questions about sorting vectors of pointers to structs
您已经确定了您的问题:
I've tried to apply the pattern in those solutions but it still doesn't work.
不要放弃那条询问线:你已经正确地确定了你需要做的事情,应该努力理解这些模式是如何工作的以及如何将它们应用到你的问题上,也许会问一个关于你在这方面的尝试的重点问题。
它将不起作用,因为您使用的是相同的签名。重载适用于不同的签名。检查您的函数签名。