how to store elements in a 2d vector and call them out.

Hi guys, i`m currently trying to implement a 2d vector to store x and y of type int.

i have successfully passed it to the function, however i am unable to store the values in it. It always returns with a segementation fault and my program terminates from there. May i know how do i store them properly and call them out?

below is my code snippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int reconstructSecret(int x, int y, vector< vector<int> > &inVec ,int constructSecret)
{
    int getX,getY,formula,accum,count,getSecret,startPosition,nextPosition,numerator,denominator;
    getX=x;
    getY=y;
    int result;
    int getSecret2=constructSecret;

    inVec.push_back(vector<int> (,getY));

    for(int i=0;i<getSecret2;i++)
    {
        for(int j=0;j<getSecret2;j++)
        {
            cout<<inVec[i][j]<<endl;
        }
    }
}




the main method
1
2
3
4
5
6
7
8
9
10
11
vector< vector<int> > inVec;

    for(int i=0;i<constructSecret;i++)
    {
        cout<<"please key in the "<<i<< "share of x value:";
        cin>>x;
        cout<<"please key in the "<<i<< "share of y value:";
        cin>>y;
        reconstructSecret(x,y,inVec,constructSecret);

    }


Thanks in advance!
What is the comma doing on line 9? Also you are attempting to call the constructor? I am not sure if that is okay to do, but I've never tried it before.
sorry about that comma, it was an edit mistake.

the updated code is here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int reconstructSecret(int x, int y, vector< vector<int> > &inVec ,int constructSecret)
{
    int getX,getY,formula,accum,count,getSecret,startPosition,nextPosition,numerator,denominator;
    getX=x;
    getY=y;
    int result;
    int getSecret2=constructSecret;

    inVec.push_back(vector<int> (getX,getY));

    for(int i=0;i<getSecret2;i++)
    {
        for(int j=0;j<getSecret2;j++)
        {
            cout<<inVec[i][j]<<endl;
        }
    }
}


i`m trying to store x and y coordinates into this 2d vector so that i can call them out to use in a formula
ok maybe i misunderstood the concept about 2D vectors.

i`m trying to store two values x and y which the user keys in
So i thought of using a 2D array , in order to store x=1 and y = 23 into a 2D vector.

so when i call out vector[0][0], it will give me x = 1 and y = 23.

does a 2D vector works this way? correct me if i`m wrong
Well, if you are creating a temporary vector container to push back, I suggest using the initializer_list braces.
e.g.
1
2
3
vector<int> Myvector({getX,getY});
//Equivalent to:
vector<int> Myvector = {getX,getY};

You can check the reference section to make sure.
nope, i still have the same segmentation fault.

this is driving me nuts lol
Did you try creating a temporary container outside that's initialized with your variables and then push that to your inVec? I'm pretty sure trying to call any constructor straight up is not good, though I don't remember why.
i shall try it in a test class.

anyway, is there anyway to store the values that i have gotten from user input

lets say x is 10 and y is 23 , i want to store them in an array that is myArray[0][0] so when i call out position 0 and 0 of the array they will return the values 10 and 23
No unfortunately. Just like with regular arrays, the container will do this:
Array of arrays --[0]--> Array of ints at position 0 --[0]--> int at position 0,0

However, you can have:
Array of arrays --[0]--> Array of ints ----> Access getX and getY stored in array.

But if for some reason you need that extra dimension, then you're going to have to use 3D vector container...
hmm ok thank you. Think i will look for other alternatives for that
Thanks for your help!
Last edited on
Topic archived. No new replies allowed.