| | |
Creating a dynamic 2D array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
I have a code snippet i've been working on but not getting very far.
What I have is a text file which looks like an XML but i want to get the data between the tags and ignore the tags themselves. I want to count the number of data items between the tags and create a 2D array so if for example I have this:
I would like to create an array 4 by 3.
I think i can get the first array right but when i try and get the other bit it gives the wrong answer
What I have is a text file which looks like an XML but i want to get the data between the tags and ignore the tags themselves. I want to count the number of data items between the tags and create a 2D array so if for example I have this:
•
•
•
•
<tag1>
Data 1
Data 2
Data 3
Data 4
</tag1>
<tag2>
test1
test2
test3
</tag2>
I think i can get the first array right but when i try and get the other bit it gives the wrong answer
C++ Syntax (Toggle Plain Text)
int d1=0, d2=1; while (! filename.eof()) //Loop through lines { getline(filename, line); string test = "<"; if(line.find("<",0) == string::npos) cout << line << endl; unsigned int pos1 = line.find(sub1, 0); unsigned int pos2 = line.find(sub2, 0); if( pos1 = string::npos) if(pos2 != string::npos) d1++; if( pos1 = string::npos) if(pos2 = string::npos) d2++; } IntArrayPtr *m = new IntArrayPtr[d1]; int i; for(i=0; i < d1; i++) m[i] = new int[d2]; cout << d1 << endl; cout << d2 << endl; filename.close();
You should consider using a vector, or a list instead of bothering with an array. It would be much easier. Read the whole file line by line in a temporary string then push it back into the vector. Iterate through the vector and you will get all of the lines one by one. Then write a function, which can determinate if it is a tag(opening or closing). If it is just throw it away, or mark it with something. When you are done, all you have to do is read out, the unmarked strings. Oops I forgot, it would only work if the tags are separated with new line, otherwise you would have to make some substraction from the strings with tag.
Last edited by BevoX; Jan 8th, 2009 at 1:50 am.
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
You should split up all the tasks into separate functions. All of it should be part of class.
The file handling would probably look like this. Unless you're concerned about massive files..
You now have the contents.
Time to just get where a tag ends and begins, continue to alter the attributes of the inner-most data based on these tags.
The file handling would probably look like this. Unless you're concerned about massive files..
C++ Syntax (Toggle Plain Text)
if(file.is_open) { while(getline(file, str)) { lines.push_back(str); } file.close(); }
Time to just get where a tag ends and begins, continue to alter the attributes of the inner-most data based on these tags.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
I did think of using seperate functions to break down each bit so i'll look into that.
Also with vectors can they be 2D?
The reason i thought an array would be better becuase i can count the number of tags so i can set the array size but i need to get wheat is in between.
Also i need to be able to reference a location of a cell and disp[lay what is in there.
I have only just started using C++ so i have no clue but i have used Java before a little
Also with vectors can they be 2D?
The reason i thought an array would be better becuase i can count the number of tags so i can set the array size but i need to get wheat is in between.
Also i need to be able to reference a location of a cell and disp[lay what is in there.
I have only just started using C++ so i have no clue but i have used Java before a little
Last edited by AdRock; Jan 8th, 2009 at 7:26 am.
Well, a vector or list is similar to an array. They are a kind of sequence containers, and they expect real arguments. So, you can't create a list or vector, which holds lists or vectors.
Read about them:
http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/stl/list/
Read about them:
http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/stl/list/
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
2D enough for you? Chris
C++ Syntax (Toggle Plain Text)
vector< vector<int> > v; for(int i = 0; i < 10; i++) v.push_back( vector<int>() ); for(int i = 0; i < v.size(); i++){ for(int x = 0; x < 10; x++){ v[i].push_back(x*i); } } for(int i = 0; i < v.size(); i++){ for(int x = 0; x < v[i].size(); x++){ cout << v[i][x] << '\t'; } cout << endl; }
Knowledge is power -- But experience is everything
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
•
•
•
•
Well, a vector or list is similar to an array. They are a kind of sequence containers, and they expect real arguments. So, you can't create a list or vector, which holds lists or vectors.
Read about them:
http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/stl/list/
![]() |
Similar Threads
- creating dynamic array (C++)
- Creating two dimensional array (C++)
- Dynamic Array of Structures, Loop problem! (C++)
- Creating dynamic array structures (C++)
Other Threads in the C++ Forum
- Previous Thread: Templates and return types
- Next Thread: extracting a sentence
Views: 1244 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int integer java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





