[英]How to sort std::set of indices according to the vector of sorted indices?
I have a class MyClass
, which operates with some double values beta
, stored as a class member, in it's member function g
.我有一个 class MyClass
,它使用一些双值beta
运行,存储为 class 成员,在它的成员 function g
中。 It sorts them and stores the permutation in the class member std::vector<int> sorted_beta_ind
:它对它们进行排序并将排列存储在 class 成员std::vector<int> sorted_beta_ind
:
double MyClass::g() {
// ...
sorted_beta_ind.resize(n);
for(unsigned int i=0; i<n; ++i) {
sorted_beta_ind[i] = i;
}
std::sort(sorted_beta_ind.begin(), sorted_beta_ind.end(),
[this] (const int &a, const int &b) {++op_cmp; return beta[a] > beta[b];});
// ...
}
Next I want to have several ordered sets of indices in another member function f
, which will store the indices in the same order as in sorted_beta_ind
.接下来我想在另一个成员 function f
中有几个有序的索引集,它将以与sorted_beta_ind
相同的顺序存储索引。 I'm trying to use std::set
objects, and as such, I need a comparator.我正在尝试使用std::set
对象,因此,我需要一个比较器。 The best solution I figured out is a lambda function我想出的最佳解决方案是 lambda function
double MyClass::f() {
auto ind_comp = [&order = sorted_beta_ind] (const int &a, const int &b) {
int pos_a = ~0, pos_b = ~0;
for(unsigned int i=0; i<order.size(); ++i) {
if(order[i] == a) {
pos_a = i;
}
if(order[i] == b) {
pos_b = i;
}
}
return pos_a < pos_b;
};
std::set<int, decltype(ind_comp)> d0, d1;
// the rest of the function which uses std::union and std::instersection
}
but on building the project I get但是在构建项目时我得到了
error: use of deleted function ‘MyClass::f()::<lambda(const int&, const int&)>& MyClass::f(int**, int)::<lambda(const int&, const int&)>::operator=(const MyClass::f()::<lambda(const int&, const int&)>&)’
Can this approach work or I should try something entirely else?这种方法可以工作还是我应该尝试完全不同的东西?
Capturing lambda expressions, like yours, are not DefaultConstructible .像您一样,捕获 lambda 表达式不是DefaultConstructible 。 And that's exactly what std::set
tries to do unless it receives a comparator object that can be copied from as a constructor call argument.这正是std::set
试图做的,除非它接收到一个比较器 object ,可以从构造函数调用参数中复制它。 That is:那是:
std::set<int, decltype(ind_comp)> d0, d1;
Here std::set
knows only the type of the comparator, and will attempt to constuct one using its default constructor.这里std::set
只知道比较器的类型,并会尝试使用其默认构造函数来构造一个。 Instead, it should be:相反,它应该是:
std::set<int, decltype(ind_comp)> d0(ind_comp), d1(ind_comp);
// ~~~~~~~^ ~~~~~~~^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.