【问题标题】:How to write custom comparator for std::minmax function for datatype vector<glm::vec3> (opengl datatype)如何为数据类型 vector<glm::vec3>(opengl 数据类型)的 std::minmax 函数编写自定义比较器
【发布时间】:2019-05-09 13:22:00
【问题描述】:

我收到以下编译器错误:

C2664:Reason: cannot convert from 'const _Ty' to 'const glm::vec3'

我的自定义比较器:

bool comp(glm::vec3   const & first, glm::vec3  const & sec) { return first.y > sec.y; }

以及我称之为的代码

 // function getVertices() returns std::vector<glm::vec3>
const std::vector<glm::vec3> verts = mesh.getVertices();
auto minmax = std::minmax(std::begin(verts), std::end(verts),comp);

如何解决这个问题?请帮忙。

【问题讨论】:

  • 请将 full 和 complete 错误输出复制粘贴到问题中。最好与minimal reproducible example 一起使用。
  • 这看起来应该可以。请提供minimal reproducible example
  • 顺便说一句,我怀疑您是否想经常复制网格的顶点。
  • 令人惊讶的是我可以reproduce in an MCVE。
  • 此代码是 openframeworks 示例的一部分。完整的代码。
      #include "ofMain.h" #include "ofApp.h" //=========== bool comp(glm::vec3 const &amp; first, glm::vec3 const &amp; sec) { return first.y &gt; sec.y; } int main( ){ ofMesh 网格; mesh.load("lofi-bunny.ply"); const std::vector<:vec3> verts = mesh.getVertices(); auto minmax = std::minmax(std::begin(verts), std::end(verts), comp); }</:vec3>

标签: c++ comparator glm-math minmax


【解决方案1】:

std::minmax 函数返回两个(或更多)值的最小值和最大值。

要使用一系列迭代器(例如容器的开头和结尾)获取最小和最大元素,您应该使用std::minmax_element。

【讨论】:

  • 其实有一个initializer_list版本,取值超过2个。
  • 但是(a)这不是一个初始化列表,即使是,(b)那么你会复制你的数据。仍然不想要。
  • 我把它改成了std::minmax_element。保持其他事情相同。有效。非常感谢。
【解决方案2】:

正如“某个程序员老兄”所建议的,我将其更改为 std::minmax_element。有效。 :)

【讨论】:

    猜你喜欢
    • 2022-08-19
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多