将元组的向量构造成堆
constructing vector of tuples into heap
从这里,我能够将元组向量转换为堆。 但是,我想进一步追溯从头开始构建元组,而无需转换向量。
我之前如何构建向量如下:
vector<tuple<int,int,int>> x;
for (int ii=0; ii < 10; ii++){
for (int jj=0; jj < 10; jj++){
y[0] = ii + rand() % 10;
y[1] = jj + rand() % 10;
y[2] = rand() % 100;
x.emplace_back(y[0], y[1], y[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++){
y[0] = ii + rand() % 10;
y[1] = jj + rand() % 10;
y[2] = rand() % 100;
x.emplace_back(y[0], y[1], y[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
相关文章:
- 将元组的向量转换/构造为堆
- 将元组的向量构造成堆
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 我正在寻找一种优雅的方式来从元组向量创建tuple_element向量
- 如何在元组初始化向量中删除样板?
- c++:交换向量中所有元组的第一个和第二个元素
- 如何在C++中创建元组向量?
- 返回两个向量 – 使用引用还是元组?
- 如何在具有特定条件的向量中删除所有元组?
- 使用 C++-17,如何从字符串向量填充元组
- C++ 按数值对元组<字符串、浮点数>然后按字典顺序排序的向量
- 如何将值元组中的相应值附加到相同类型的向量元组中
- 将可变参数模板包装到元组向量
- 从 c++ 中的字符串向量创建一个元组
- C++向量元组,请按索引从元素创建元组
- 从类型列表创建向量元组
- 如何打印类型向量<元组<字符串、int、int>> 以筛选 c++?
- 如何使用 C++11 可变参数模板来定义由向量元组支持的元组向量
- 初始化类成员初始值设定项列表中的向量元组
- 如何使用笛卡尔积将向量元组转换为元组的向量