从这里,我能够将元组向量转换为堆。 然而,我想进一步回到从头开始构造元组,从而消除转换向量的需要。
我之前构造向量的方式如下:
vector<tuple<int,int,int>> x;
for (int ii=0; ii < 10; ii++){
for (int jj=0; jj < 10; jj++){
x[0] = ii + rand() % 10;
x[1] = jj + rand() % 10;
x[2] = rand() % 100;
x.emplace_back(x[0], x[1], x[2]);
}
}
我是如何尝试从头开始构造一个堆
struct Comp {
bool operator()(const tuple<int,int,int>& a,
const tuple<int,int,int>& b){
return (get<2>(a) < get<2>(b));
}
};
vector<tuple<int,int,int>> x;
for (int ii=0; ii < 10; ii++){
for (int jj=0; jj < 10; jj++){
x[0] = ii + rand() % 10;
x[1] = jj + rand() % 10;
x[2] = rand() % 100;
x.emplace_back(x[0], x[1], x[2]);
push_heap(x.begin(), x.end(), Comp()); // using push_heap
}
}
PUSH_HEAP()
行出错:
Severity Code Description Project File Line Suppression State
Error C2228 left of '.begin' must have class/struct/union
Error (active) E0153 expression must have class type
Error (active) E0153 expression must have class type
Error C2780 'void std::push_heap(_RanIt,_RanIt,_Pr)': expects 3 arguments - 2 provided
Error C2672 'push_heap': no matching overloaded function found
您使用x
作为堆和元组的名称。 加operator[]
不是访问元组字段的方法。 另外,您正在多次尝试创建堆
我猜你的意思是这样的
for (int ii=0; ii < 10; ii++){
for (int jj=0; jj < 10; jj++){
tuple<int,int,int> y;
get<0>(y) = ii + rand() % 10;
get<1>(y) = jj + rand() % 10;
get<2>(y) = rand() % 100;
x.emplace_back(y);
}
}
push_heap(x.begin(), x.end(), Comp()); // using push_heap
或者更简单一点
for (int ii=0; ii < 10; ii++){
for (int jj=0; jj < 10; jj++){
x.emplace_back(ii + rand() % 10, jj + rand() % 10, rand() % 100);
}
}
push_heap(x.begin(), x.end(), Comp()); // using push_heap