std::equal_range 函数与排列

huangapple go评论72阅读模式
英文:

std::equal_range with a permutation

问题

我正在尝试使用std::equal_range来查找已使用单独的索引数组排序的向量中的相等元素。但出于某种原因,std::equal_range找不到元素。

这是一个示例。

#include <algorithm>
#include <iostream>
#include <vector>

struct compare {
  bool operator()(float value, int index) const { return value < values[permutation[index]]; }
  bool operator()(int index, float value) const { return values[permutation[index]] < value; }
  const std::vector<float>& values;
  const std::vector<int>& permutation;
};

int main() {
  std::vector<float> values = { 1., 2., 2., 4., 6., 5. };
  std::vector<int> permutation = { 0, 1, 2, 3, 5, 4 };

  const auto p = std::equal_range(permutation.begin(), permutation.end(), 6., compare{values, permutation});
  for (auto i = p.first; i != p.second; ++i) {
    std::cout << *i << '\n';
  }
}

我期望索引对应值6被打印出来。不确定我漏掉了什么。

英文:

I am trying to use std::equal_range to find equal elements in a vector that has been sorted using a separate index array. For some reason, std::equal_range cannot find the element.

This is an example.

#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

struct compare {
  bool operator()(float value, int index) const { return value &lt; values[permutation[index]]; }
  bool operator()(int index, float value) const { return values[permutation[index]] &lt; value; }
  const std::vector&lt;float&gt;&amp; values;
  const std::vector&lt;int&gt;&amp; permutation;
};

int main() {
  std::vector&lt;float&gt; values = { 1., 2., 2., 4., 6., 5. };
  std::vector&lt;int&gt; permutation = { 0, 1, 2, 3, 5, 4 };

  const auto p = std::equal_range(permutation.begin(), permutation.end(), 6., compare{values, permutation});
  for (auto i = p.first; i != p.second; ++i) {
    std::cout &lt;&lt; *i &lt;&lt; &#39;\n&#39;;
  }
}

I expect that the index to value 6 is printed. Not sure what I'm missing.

答案1

得分: 1

Replace values[permutation[index] in the compare operator with values[index].

英文:

You've confused your indexing. permutation.begin() and permutation.end() will return the values of permutation, so when you use those values in your comparator, it's not what you expect. e.g. permutation.begin() points to 0, indicating the first element of the values vector. However, when you use it in the comparison, you're using it to index into the permutation vector (again)

Replace values[permutation[index] in the compare operator with values[index]

答案2

得分: 0

传递给比较器的值是permutation的元素,而不是permutation的索引。如果相应地更改比较器,您将获得预期的结果:

#include <algorithm>
#include <iostream>
#include <vector>

struct compare {
  bool operator()(float value, int index) const { return value < values[index]; }
  bool operator()(int index, float value) const { return values[index] < value; }
  const std::vector<float>& values;
  const std::vector<int>& permutation;
};

int main() {
  std::vector<float> values = { 1., 2., 2., 4., 6., 5. };
  std::vector<int> permutation = { 0, 1, 2, 3, 5, 5, 4 };

  const auto p = std::equal_range(permutation.begin(), permutation.end(), 6., compare{values, permutation});
  for (auto i = p.first; i != p.second; ++i) {
    std::cout << *i << '\n';
  }
}

这将打印4,因为values[permutation[5]] == values[4] == 6.

Live Demo

英文:

The values that get passed to the comparator are the elements of permuatation not indices into permutation. If you change the comparator accordingly you get the expected result:

#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

struct compare {
  bool operator()(float value, int index) const { return value &lt; values[index]; }
  bool operator()(int index, float value) const { return values[index] &lt; value; }
  const std::vector&lt;float&gt;&amp; values;
  const std::vector&lt;int&gt;&amp; permutation;
};

int main() {
  std::vector&lt;float&gt; values = { 1., 2., 2., 4., 6., 5. };
  std::vector&lt;int&gt; permutation = { 0, 1, 2, 3, 5, 5, 4 };

  const auto p = std::equal_range(permutation.begin(), permutation.end(), 6., compare{values, permutation});
  for (auto i = p.first; i != p.second; ++i) {
    std::cout &lt;&lt; *i &lt;&lt; &#39;\n&#39;;
  }
}

This will print 4 because values[permutation[5]] == values[4] == 6..

Live Demo

huangapple
  • 本文由 发表于 2023年2月24日 04:26:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定