3D 矢量 - "No instance of overload function?"

3D Vector - "No instance of overload function?"

本文关键字:overload function of No 矢量 3D instance      更新时间:2023-10-16

C 中的向量仍然相对较新,此函数的目的是进行4个参数,其中3个定义了正在编写的数据的(x,y,z(位置,并且第四是要编写的价值。

根据要求,列出了错误的图片:

上面列出的代码的图片

问题在" push_back"代码下。这 "。"yy.push and xx.push给出错误"没有超载函数的实例"。

如果有人可以解释这意味着什么以及如何解决它,我将非常感谢它!:(

double datawrite(vector<unsigned int> xx, vector<unsigned int> yy, 
vector<unsigned int> zz, double val) {
//Writes data to the 3d Vector
//finds coordinates for data
    vector< vector< vector<unsigned int > > > xx;
    vector< vector<unsigned int> > yy;
    vector<unsigned int> zz;
//Writes value at proper position
    zz.push_back(val);
    yy.push_back(zz);
    xx.push_back(yy);
    //outputs value from vector
    return val;
}

所以您想要一个双打的3D矩阵吗?首先,您需要创建它:

#include <vector>
std::vector<vector<vector<double>>> matrix;

这会创建一个3D矩阵,但具有0个大小。接下来,当您将数据添加到矩阵中时,您需要确保矩阵足够大:

// Co-ords are integers
double datawrite(int x, int y, int z, double val)
{
    // Make sure vectors are large enough
    if (matrix.size() < x+1) matrix.resize(x+1);
    if (matrix[x].size() < y+1) matrix[x].resize(y+1);
    if (matrix[x][y].size() < z+1) matrix[x][y].resize(z+1);
    // Store the value
    matrix[x][y][z] = val;
    return val;
}

但是,这有点混乱,并且矩阵处于不完整状态。例如,如果您调用datawrite(2, 3, 4, 9.9);,则可能会显示所有索引&lt;2,3,4是有效的,但事实并非如此。例如,尝试阅读matrix[0][0][0]会给您带来错误。

您可以使用dataread函数来解决此问题,该功能在尝试从它们中读取之前检查向量的大小。

如果您提前知道矩阵有多大,则可以立即创建整个矩阵:

vector<vector<vector<double>>> matrix(10, vector<vector<double>>(10, vector<double>(10)));

这将创建一个完整的10x10x10矩阵。这确保了所有索引&LT;10将有效。我更喜欢这种方法。然后您的功能变为:

double datawrite(int x, int y, int z, double val)
{
    // Make sure indexes are valid
    if (x >= matrix.size() || y >= matrix[x].size() || z >= matrix[x][y].size()) {
        // Up to you what to do here.
        // Throw an error or resize the matrix to fit the new data
    }
    // Store the value
    matrix[x][y][z] = val;
    return val;
}