tags:

views:

160

answers:

2

Hello, I would like to use a 3-d Vector to store and add values between some calculations in c++. I'm having problems adding the third dimension to my vector. What I would like to achieve is a vector that for each iteration puts in a 2-D vector and here only the first values for each vector... So The input would look something like this

1 3 7 9
- - - -

Then Later on I would like to add values to the places marked with - so in the end the matrix would look something like this(for every iteration)(only 2-d shown...)

1 3 7 9
2 5   7
3     2
1

Right now I'm having trouble adding the first elements to it. And i'm using the sollist 3-D vector as a global vector. My values array all have the same amount of elements that are > 0.5 so that's not where the error is.

 vector<vector<vector<int>>>sollist;


void sol(array& values, int& iter)
    {int i;
    sollist.push_back ( vector<vector<int>>() );

        for (i=0;i<10;i++)
    if (values[i]>0.5)
    sollist[iter][0].push_back(i);
    }

Thank you very much for any help and for an excellent forum... /Buxley

+2  A: 

I think you have to do something like this.

sollist.push_back(vector<vector<int>>());
sollist[0].push_back(vector<int>());
sollist[0][0].push_back(value);
Razvi
Yes, That was it...Thanks...I just became blind after watching my problem for a couple of hours...
mrbuxley
+4  A: 

I really think you would be better off using an existing matrix library tthan taking this a approach - there are quite a few to choose from, Google for "C++ matrix library". If you must roll your own, you should definitely implement your own Matrix class rather than messing around with naked vectors.

anon
I don't know the size of my final 3-D Array and therefore thought I would be better off using Vectors... I don't know if that's the case. Is there any other problem with my approach than that it's a bit messy?
mrbuxley
Most matrix libraries do not support what mrbuxley want. I know what he calls a "3-d vector" as a tensor, but the usual definition for tensors requires it to be regular, while mrbuxley wants the 1-d vectors making up the 3-d vectors to have different length. It may be possible to base a solution of Matrix<vector> where Matrix is from an existing matrix library but then I wonder why to use an existing library if you only need a trivial part of it.
Jitse Niesen