C++ 在堆中构造元组向量

C++ 在堆中构造元组向量,c++,vector,tuples,heap,C++,Vector,Tuples,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;

从中,我能够将元组向量转换为堆。 然而,我想更进一步,从头开始构造元组,消除了转换向量的需要

我之前是如何构造向量的,如下所示:

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]);
    }
}
您正在使用x作为堆和元组的名称。Plus运算符[]不是访问元组字段的方法。另外,您正在多次尝试创建堆

我猜你的意思是这样的

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
您正在使用x作为堆和元组的名称。Plus运算符[]不是访问元组字段的方法。另外,您正在多次尝试创建堆

我猜你的意思是这样的

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

请在问题中包含a请在问题中包含a我是否误解了push_heap的功能?我的印象是,它是一个基于元素的每元组操作,因此我将其包含在for循环中。事实上,你是对的,我的疏忽是在x.emplace_backx[0],x[1],x[2];它应该是x.emplace_backy[0],y[1],y[2];相反,我是不是误解了push_heap的功能了?我的印象是,它是一个基于元素的每元组操作,因此我将其包含在for循环中。事实上,你是对的,我的疏忽是在x.emplace_backx[0],x[1],x[2];它应该是x.emplace_backy[0],y[1],y[2];相反