carl.alv

November 21st, 2007, 12:24 PM

Hi all!

I have this function which takes as an input two arrays of vectors declared as

vector<int> v1[N], v2[N];

The function is

void fun3(Base** b, vector<int>* v1, vector<int>* v2, int N){

for(int i = 0; i < N; i++){

v1[i].clear();

v2[i].clear();

}

int cont = 0;

for(int i = 0; i < N-1; i++){

int vali = b[i]->get_priv();

for(int j = i+1; j < N; j++){

int valj = b[j]->get_priv();

int dif = vali - valj;

if(dif < 0){

v1[i].push_back(j);

cout << j << " " << v1[i][cont] << " ";

v2[j].push_back(i);

cout << i << " " << v2[j][cont] << endl;

cont++;

}

}

cout<<endl;

}

}

which for N = 4 I expect to print

1 1 0 0

2 2 0 0

3 3 0 0

2 2 1 1

3 3 1 1

3 3 2 2

but instead i'm getting

1 1 0 0

2 2 0 0

3 3 0 0

2 0 1 0

3 0 1 0

3 0 2 0

I suppose that there is a memory allocation problem due to my wrong implementation of the vector array, but I don't know where is my fault. Can someone help me??

I have this function which takes as an input two arrays of vectors declared as

vector<int> v1[N], v2[N];

The function is

void fun3(Base** b, vector<int>* v1, vector<int>* v2, int N){

for(int i = 0; i < N; i++){

v1[i].clear();

v2[i].clear();

}

int cont = 0;

for(int i = 0; i < N-1; i++){

int vali = b[i]->get_priv();

for(int j = i+1; j < N; j++){

int valj = b[j]->get_priv();

int dif = vali - valj;

if(dif < 0){

v1[i].push_back(j);

cout << j << " " << v1[i][cont] << " ";

v2[j].push_back(i);

cout << i << " " << v2[j][cont] << endl;

cont++;

}

}

cout<<endl;

}

}

which for N = 4 I expect to print

1 1 0 0

2 2 0 0

3 3 0 0

2 2 1 1

3 3 1 1

3 3 2 2

but instead i'm getting

1 1 0 0

2 2 0 0

3 3 0 0

2 0 1 0

3 0 1 0

3 0 2 0

I suppose that there is a memory allocation problem due to my wrong implementation of the vector array, but I don't know where is my fault. Can someone help me??