【问题标题】:Sorting a vector of custom class doesn't iterate over the vector correctly对自定义类的向量进行排序不会正确迭代向量
【发布时间】:2021-02-24 22:54:25
【问题描述】:

我正在尝试为渲染分配实现画家排序算法。代码的前提是我需要找到一个多边形的平均深度,以及通过for循环分配给它们的深度来找到多边形的列表。

这是多边形声明,以及多边形后变换的顶点集合,用于计算多边形的深度

std::vector<Polygon3D> _polygons;
std::vector<Vertex> _transvertices;

这是模型类调用的方法,用于使用 std::sort 对 _polygons 向量进行排序

void Model::Sort()
{
    for (int i = 0; i <= GetPolygonCount(); i++)
    {
        _polygons[i].SetDepth((_transvertices[_polygons[i].GetIndex(0)].Get(2) + _transvertices[_polygons[i].GetIndex(1)].Get(2) + _transvertices[_polygons[i].GetIndex(2)].Get(2)) / 3);
    }

    sort(_polygons.begin(), _polygons.end(), sortByDepth);

}

然后这段代码链接到这个二元谓词

bool sortByDepth(const Polygon3D &lhs, const Polygon3D &rhs)
{
    float m = lhs.GetDepth(); //For value testing
    float n = rhs.GetDepth(); //For value testing
    return lhs.GetDepth() > rhs.GetDepth();
}

问题是,一旦排序算法开始,lhs 和 rhs 的值永远不会改变 - lhs 的深度始终为 0(进一步研究它的分配,它似乎正在创建一个全新的多边形?)和 rhs始终具有 30.53 的值(_polygons 顶点中第一个多边形的深度

我担心问题可能在于没有一种形式的迭代器链接到 Polygon3D 类,但我不知道从哪里开始为该类创建迭代器。

任何帮助将不胜感激,我已经查看了太多类似的问题,但似乎没有一个完全适合我的特定问题。

编辑:

帖子被撤下是因为我显然没有提供足够的代码。我试图在另一个项目中重现该问题,但由于某种原因它在那里迭代得很好。

这是我尝试过的“最短的复制”,但由于某种原因,这似乎与原版没有相同的问题。

#include <vector>
#include <algorithm>

class Polygon3D
{
public:
    Polygon3D(); // Example data for testing purposes
    float GetDepth() const;
    void SetDepth(float depth);
private:
    float _depthAverage;
};

class Model
{
public:
    Model();
    size_t GetPolygonCount() const;
    void Sort();
private:
    std::vector<Polygon3D> _polygons;
    std::vector<int> _vertices;
    std::vector<int> _transvertices;
};


Polygon3D::Polygon3D()
{
    //_depthAverage = float(rand() % 100);
}

float Polygon3D::GetDepth() const
{
    return _depthAverage;
}

void Polygon3D::SetDepth(float depth)
{
    _depthAverage = depth;
}

Model::Model()
{
    for (int i = 0; i < 10; i++)
    {
        _polygons.push_back(Polygon3D());
    }

    this->Sort();
}

size_t Model::GetPolygonCount() const
{
    return _polygons.size() - 1;
}

bool sortByDepth(const Polygon3D& lhs, const Polygon3D& rhs)
{
    float m = lhs.GetDepth();
    float n = rhs.GetDepth();
    return lhs.GetDepth() > rhs.GetDepth();
}

void Model::Sort()
{
    for (int i = 0; i <= GetPolygonCount(); i++)
    {
        _polygons[i].SetDepth(float(rand() % 100) / 3);
    }

    sort(_polygons.begin(), _polygons.end(), sortByDepth);

}

int main()
{
    Model m = Model();
}

编辑 2:

我只是使用自动类型变量来手动迭代_polygons,这似乎有效。我不明白为什么 std::sort 没有

auto begin = _polygons.begin();

    while(true)
    {
        begin++;
    }

【问题讨论】:

  • i &lt;= GetPolygonCount() - 只是为了我自己的理智,为什么不在 _polygons 上使用迭代器。同样的,如果GetPolygonCount()_polygons.size() 的同义词,那么您的循环将超出您的容器上限。
  • 欢迎来到 Stack Overflow!请edit您的问题与minimal reproducible exampleSSCCE (Short, Self Contained, Correct Example)
  • 编译器是否可能将std::sort 与您定义的sort 混淆?你在排序之前检查过深度是否正确吗?你有多少个多边形?设置深度的循环也应该有i &lt; GetPolygonCount()
  • 作业是给学校的 - 我被指示制作 GetPolygonCount() 方法并使用 std::sort 方法,所以我不能只编写自己的排序方法来解决这个问题。我检查了,但 std::sort 绝对是在使用的。

标签: c++ class sorting graphics iteration


【解决方案1】:

结果证明我自己的答案非常愚蠢。问题在于 Polygon3D 类中使用的复制构造函数 - 我忘记在复制构造函数中复制深度值,这意​​味着 lhs 没有获得深度值。

【讨论】:

    猜你喜欢
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多