Although this may seem like a question posed somewhere else, I couldn't find an appropriate answer to my question. So I am asking here for help!
Problem: I want to throw my data into either a multidimensional array or a vector. The problem is that it has different length for each line. The data (in txt file) I have is element output from gmsh. I am trying to build a finite element method solver. It looks like this:
1 15 2 1 1 1 (6 numbers)
2 15 2 1 2 2
3 1 2 99 1 1 5 (7 numbers)
4 1 2 99 1 5 6
...
...
842 2 2 100 6 170 413 337 (8 numbers)
843 2 2 100 6 146 413 343
844 2 2 100 6 155 396 377
The data is organized in the following manner.
[index] [type] [tag1] [tag2] [tag3] [node1] ... [nodeN]
As you can see from the data, index, type, tag1, tag2, tag3 are common in all lines whereas the number of node data varies depending on the type. For example, type 15 has 1 node, type 1 has 2 nodes, type 2 has 3 nodes.
My initial attempt was to put each line in an object and then put them in a vector using push_back().
I did so because the data will be different from problems to problems, but it was not a good way to handle what I have. As long as I can organize the data, array or vector doesn't matter to me.
Can someone please help me?
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
|
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
typedef struct{
int eNum;
int type;
int tag1;
int tag2;
int tag3;
int nodei;
int nodej;
int nodek;
}Elem;
istream& elemVec(istream& in, vector<Elem>& vec) {
if(in){
vec.clear();
Elem e;
while (in >> e.eNum >> e.type >> e.tag1 >> e.tag2 >> e.tag3 >> e.nodei >> e.nodej >> e.nodek){
vec.push_back(e);
}
in.clear();
}
return in;
}
|