Hello,

I need to store the cooridants of a vector, for example:

If the vector was:

0 1 0 1 0
0 1 0 1 0
0 1 1 1 1
0 0 0 0 0

And this vector is then split into blocks, I need to store the the corridents so I can then use them later to put the vector back together. Now I have managed to get the calculation to calculate the corridents, I just don't know how I'd go about setting them as a 2D vector..

Here is the function that handles the corridents:

``````vector<double> subsetMatrix(const iniMatrix& theMatrix, int mat_row, int mat_col, int offsetX, int offsetY, int newRow, intnewCol)
{
iniMatrix newMatrix;

for(int i=offsetY; (i < newRow); i++)
{
for(int j= offsetX; (j < newCol); j++)
{
newMatrix.push_back(theMatrix[i*mat_row+j]);
// i*mat_row+j IS the corridents
}
}
return newMatrix;
}
``````

I have a 2D vector defined in the class, I just need a way of pushing to this vector and then retriving it.

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by Labdabeta

Can you maybe post what your iniMatrix class looks like? Also just to maybe catch any premature problems here is some sample vector of vector code:

``````vector<vector<type> /*note the space that must be here*/> vec;
vector<type> newVec;//this can be set to actually hold something;
vec.push_back(newVec);
vector<type> backVec=vec.back();//this will return the whole vector
///Fake 1D version (in a class)
template<typename T>
class vec2D
{
private:
vector<T> vec;
int w;
T zero;//this is the default vector value
public:
vec2D(int h, int w, int v):w(w),zero(v)
{
for (int i=0; i<w*h; ++i)//set the vector to {v}
vec.push_back(v);
}
T &operator()(int x, int y)
{
if (x*y>=vec.size())
{
//we need to reserve more space!
if (x>w)
{
for (int xx=0; xx<x; ++xx)
{
for (int i=0; i<vec.size()/w; ++i)
vec.insert(vec.begin()+i*w,zero);
w=x;
}
}
if (y>vec.size()/w)
{
for (int yy=0; yy<y; ++yy)
{
for (int i=0; i<w; ++i)
vec.push_back(zero);
}
}
}
return vec[y*w+x];
}
};
``````

NOTE: I may have made a mistake with my class and it could definately use some more functionality, it is merely for showing you how you COULD make a fake 2D-vector class.

Edited by Dani: Formatting fixed

Sorry, I am still getting used to the new posting system, I guess I didn't do the code right. I cannot seem to figure out how to edit it either.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.