简体   繁体   English

向量 <vector <double> &gt;运营商

[英]vector <vector <double>> operator

I want to modify a 我想修改一个

vector <vector <double>>

declared in main() from another function. 在另一个函数的main()声明。
I pass the pointer to the vector as a function parameter; 我将指针作为函数参数传递给向量; unfortunately, as soon as I try to modify the vector I get a segmentation error. 不幸的是,一旦我尝试修改向量,我就会出现分段错误。

Originally this was my code: 最初这是我的代码:

#include <vector>
#include <iostream>

using namespace std;
void function(vector <vector <double>> *result, int k){
    double r = 0.2;
    for(int i=0;i<5;i++){
        r = r*0.2;
        result[k][i].push_back(r);
    }
}

int main(){
    vector <vector <double>> result;
    for(int i=0;i<5;i++) function(&result, i);

    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++) cout << result[i][j] << " ";
        cout << " " << endl;
    }
    return 0;
}

The compilation with "g++" went good but I got segmentation error when I ran the program. 使用“g ++”的编译很顺利,但是当我运行程序时出现了分段错误。
I think the problem is in the use of the operator [] since I am applying it to a pointer, so I might need something like: 我认为问题在于使用operator [],因为我将它应用于指针,所以我可能需要这样的东西:

result->operator[](k)->operator[](i).push_back(r);

which I tried but unfortunately doesn't compile. 我试过但不幸的是没有编译。

EDIT: I was using "push_back()" on result[k][i] which is a double. 编辑:我在结果[k] [i]上使用“push_back()”这是一个双倍。

I modified: 我修改了:

result[k][i].push_back(r);

in: 在:

res[k][i]=r;

but I still get a segmentation error :/ 但我仍然得到分段错误:/

EDIT 2: The problem was in the declaration of the vector of vectors; 编辑2:问题在于向量向量的声明; I didn't specify a size. 我没有指定尺寸。 It should look like: 它应该看起来像:

vector <vector <double>> result(int size);

Just pass the vector by reference: 只需通过引用传递向量:

//                     -------------v
void function(vector<vector<double>>& res, int k){
    //some calculations
    for(int i=0;i<5;i++){
        //calculations which give r (double) as result
        res[k].push_back(r);
    }
}

If you really want to pass it by pointer, then you have to dereference it: 如果你真的想通过指针传递它,那么你必须取消引用它:

void function(vector<vector<double>>* res, int k){
    //some calculations
    for(int i=0;i<5;i++){
        //calculations which give r (double) as result
        (*res)[k].push_back(r);
    }
}

And as @gambinaattori says, you cannot push back into a double , you push back into the vector in the outermost vector. 正如@gambinaattori所说,你无法推回到double ,你会回到最外层矢量中的矢量。

You'll also need to first vector to be filled with vectors. 你还需要第一个向量来填充向量。 If the first level of vector is empty, you cannot push back the second level since there is none. 如果第一级向量为空,则无法推回第二级,因为没有。

Initializing the vector of vector with a bunct of empty vectors in it is done like that: 在其中初始化带有一个空矢量的矢量矢量就像这样:

std::vector<std::vector<double>> result(5);

Now there is 5 empty vectors. 现在有5个空矢量。

Then to pass by reference you need drop the & symbol from the arguments: 然后通过引用传递,您需要从参数中删除&符号:

function(result, i);

Here's a live example . 这是一个实例

In your example res[k][i] is a double, not a vector, so calling push_back on it screws you. 在你的例子中, res[k][i]是一个double,而不是一个向量,所以在它上面调用push_back会让你感到困惑。 Call res[k].push_back(r) instead or just write res[k][i] = r . 改为调用res[k].push_back(r)或者只写res[k][i] = r Also, you may want to pass the vector by a reference instead of a pointer. 此外,您可能希望通过引用而不是指针传递向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM