英文:
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 <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';
}
}
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.
。
英文:
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 <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';
}
}
This will print 4
because values[permutation[5]] == values[4] == 6.
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论