Creating a dynamic 2D array

Reply

Join Date: Dec 2008
Posts: 55
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Creating a dynamic 2D array

 
0
  #1
Jan 7th, 2009
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:

<tag1>
Data 1
Data 2
Data 3
Data 4
</tag1>
<tag2>
test1
test2
test3
</tag2>
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

  1. int d1=0, d2=1;
  2.  
  3. while (! filename.eof()) //Loop through lines
  4. {
  5. getline(filename, line);
  6.  
  7. string test = "<";
  8. if(line.find("<",0) == string::npos)
  9. cout << line << endl;
  10.  
  11. unsigned int pos1 = line.find(sub1, 0);
  12. unsigned int pos2 = line.find(sub2, 0);
  13.  
  14. if( pos1 = string::npos)
  15. if(pos2 != string::npos)
  16. d1++;
  17.  
  18. if( pos1 = string::npos)
  19. if(pos2 = string::npos)
  20. d2++;
  21.  
  22.  
  23. }
  24.  
  25. IntArrayPtr *m = new IntArrayPtr[d1];
  26.  
  27. int i;
  28.  
  29. for(i=0; i < d1; i++)
  30. m[i] = new int[d2];
  31.  
  32. cout << d1 << endl;
  33. cout << d2 << endl;
  34.  
  35. filename.close();
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 57
Reputation: BevoX is on a distinguished road 
Solved Threads: 12
BevoX's Avatar
BevoX BevoX is offline Offline
Junior Poster in Training

Re: Creating a dynamic 2D array

 
0
  #2
Jan 8th, 2009
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."
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 922
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Creating a dynamic 2D array

 
0
  #3
Jan 8th, 2009
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..
  1. if(file.is_open)
  2. {
  3. while(getline(file, str))
  4. {
  5. lines.push_back(str);
  6. }
  7.  
  8. file.close();
  9. }
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.
"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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 55
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Re: Creating a dynamic 2D array

 
0
  #4
Jan 8th, 2009
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
Last edited by AdRock; Jan 8th, 2009 at 7:26 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 57
Reputation: BevoX is on a distinguished road 
Solved Threads: 12
BevoX's Avatar
BevoX BevoX is offline Offline
Junior Poster in Training

Re: Creating a dynamic 2D array

 
0
  #5
Jan 8th, 2009
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/
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Creating a dynamic 2D array

 
0
  #6
Jan 8th, 2009
2D enough for you?
  1. vector< vector<int> > v;
  2.  
  3. for(int i = 0; i < 10; i++)
  4. v.push_back( vector<int>() );
  5.  
  6. for(int i = 0; i < v.size(); i++){
  7. for(int x = 0; x < 10; x++){
  8. v[i].push_back(x*i);
  9. }
  10. }
  11.  
  12. for(int i = 0; i < v.size(); i++){
  13. for(int x = 0; x < v[i].size(); x++){
  14. cout << v[i][x] << '\t';
  15. }
  16. cout << endl;
  17. }
Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Creating a dynamic 2D array

 
0
  #7
Jan 9th, 2009
Originally Posted by BevoX View Post
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/
That's just wrong information! A vector can hold anything - its a freakin template class!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 57
Reputation: BevoX is on a distinguished road 
Solved Threads: 12
BevoX's Avatar
BevoX BevoX is offline Offline
Junior Poster in Training

Re: Creating a dynamic 2D array

 
0
  #8
Jan 11th, 2009
Originally Posted by skatamatic View Post
That's just wrong information! A vector can hold anything - its a freakin template class!
Yeah now I know. I am sorry I didn't want to mislead anyone.
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC