how to arrange this data?

hi guys...i'm a new member here..i have a problem in my work and i hope anyone here can help me.

i have a data and this data actually is read from .txt file format. here, i give u an example of this data.

0 1
0 7
1 0
1 2
2 1
2 3
2 5
3 2
4 5
4 7
5 2
5 4
6 7
7 0
7 4
7 6

left side is the index of mesh and the right side is the neighbor for each mesh
what i want is, i want to arrange this data to be like this. for each mesh, it will find its own neighbor.

for example, index mesh 0. 0 has two neighbor.there are 1 and 7 whereas mesh 2 has 3 neighbors. there are 1 3 5. so the data that i want must be like this (below)..

0 1 7
1 0 2
2 1 3 5
3 2
4 5 7
5 2 4
6 7
7 0 4 6

i already code the algorithm..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
void readdata3()
{
ifstream infile;
infile.open("nei.txt");

f = new int*[p];
for(int i=0;i<p;i++)
f[i] = new int [2];

g = new int*[p];
for(int j=0;j<p;j++)
g[j] = new int [3];

for(int i=0;i<p;i++)
{
infile >> f[i][0] >> f[i][1]; 
}


for(int j=2 ; j< p ;j++)
{


if((f[j][0] != f[j-1][0]) && (f[j-1][0] != f[j-2][0]) && (f[j][0] != f[j-2][0]))
{
g[j][0] = f[j-1][1];


cout << g[j][0] ;
}

else if((f[j][0] == f[j-1][0]) && (f[j-1][0] != f[j-2][0]) && (f[j-2][0] != f[j][0]))
{
g[j][0] = f[j-2][1];

cout << g[j][0] ;
}

else if((f[j][0] == f[j-1][0]) && (f[j-1][0] == f[j-2][0]) && (f[j-2][0] == f[j][0]))
{
g[j][0] = f[j-2][1];
g[j][1] = f[j-1][1];
g[j][2] = f[j][1];
cout << g[j][0] << " " << g[j][1]<< " " << g[j][2] <<endl;
}

else if((f[j-1][0] == f[j-2][0]) )
{
g[j][0] = f[j-2][1];
g[j][1] = f[j-1][1];
cout << g[j][0] << " " << g[j][1] << " " << endl;
}

}

}



but this program cannot give what i want...it has a problem to read the mesh that have a same value in the previous mesh and previous-previous mesh (don't know how to say)...

let say, i=9, then if the value of f[8] = f[7]...that it will return the value of g[i] = f[7], g[i+1] = f[8]...for certain cases it work..but for some cases, it don't..i stuck here..

i hope anyone can help me. i really appreciate ur help...

thanks buddy.. :(
Last edited on
I think it would be easier for you if you used a map< int, vector<int> > to hold the data.
Declare one like this: map< int, vector<int> > my_mesh_list; and then make the file-reading loop like this:

1
2
3
4
5
6
7
8
9
int m1, m2;
while (true)
{
    infile>>m1>>m2;

    if (!infile) break;

    my_mesh_list[m1].push_back(m2);
}

Info on map -> http://cplusplus.com/reference/stl/map/
Info on vector -> http://cplusplus.com/reference/stl/vector/

EDIT: Here is a working example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
using namespace std;

int main()
{
    //reading...
    ifstream fin("in.txt");

    map<int,vector<int> >my_mesh_list;

    int m1, m2;
    while (true)
    {
        fin>>m1>>m2;

        if (!fin) break;

        my_mesh_list[m1].push_back(m2);
    }

    fin.close();

    //writing...
    ofstream fout("out.txt");

    map<int,vector<int> >::iterator it;
    int i;
    int size;

    for (it=my_mesh_list.begin(); it!=my_mesh_list.end(); it++)
    {
        fout << it->first << ' ';

        size=it->second.size();
        for (i=0; i<size; i++)
        {
            fout << it->second[i] << ' ';
        }

        fout << endl;
    }

    fout.close();

    return 0;
}

in.txt:
0 1
0 7
1 0
1 2
2 1
2 3
2 5
3 2
4 5
4 7
5 2
5 4
6 7
7 0
7 4
7 6

out.txt:
0 1 7 
1 0 2 
2 1 3 5 
3 2 
4 5 7 
5 2 4 
6 7 
7 0 4 6 
Last edited on
thanks master. i already try ur idea code into my others data...it's work...thank you very much!!!!!!!.......
Topic archived. No new replies allowed.