creating dynamic array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

creating dynamic array

 
0
  #1
Aug 24th, 2007
I m reading a text file which is of format
L1,L2,L3
L1,0,0,0
L2,1,0,0
L3,1,0,0

using getline method. The first lines signifies the vertex of a graph. Now from the second line onwards it signifies edges.
Now from second line onwards i want to add the elements in a dynamic array through vector so that i can create edges.
Kindly suggest the method for that..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: creating dynamic array

 
0
  #2
Aug 24th, 2007
vector does all the dynmaic allocation for you -- just call its push_back() method to add a new row.
  1. string line;
  2. vector<string> array;
  3. ...
  4. // now put this inside the file read loop just after getline()
  5. array.push_back(line);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Re: creating dynamic array

 
0
  #3
Aug 27th, 2007
That's true but i have to also make sure that if the value is 1 then i have to create edges between the given vertex.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: creating dynamic array

 
0
  #4
Aug 27th, 2007
Maybe a linked list of some sort may be a better idea?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Re: creating dynamic array

 
0
  #5
Aug 27th, 2007
How do i create a 2-D array using a vector which is dynamic in nature. Also let me know if how we can make use of "find" in this case.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: creating dynamic array

 
0
  #6
Aug 27th, 2007
Originally Posted by tonyaim83 View Post
How do i create a 2-D array using a vector which is dynamic in nature.
You can make use of vector<vector<T> >
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: creating dynamic array

 
0
  #7
Aug 27th, 2007
you can repesent a graph as an adjacency matrix. http://www.cs.usask.ca/resources/tut...es/matrix.html
using the specialization vector<bool> would take only one bit per matrix element, so large graphs can be accommodated. (not withstanding my efforts, the daniweb formatter refuses to believe that this sentence is not a url)
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <boost/algorithm/string.hpp>
  6. #include <cassert>
  7. using namespace std ;
  8. using namespace boost ;
  9.  
  10. typedef vector< vector<bool> > adjacency_matrix ;
  11.  
  12. int main()
  13. {
  14. vector<string> vertices ;
  15. ifstream file( "whatever.txt" ) ;
  16. string line ;
  17.  
  18. // read and parse the first line
  19. assert( getline( cin, line ) ) ;
  20. split( vertices, line, is_any_of(", ") );
  21.  
  22. const size_t N = vertices.size() ;
  23. adjacency_matrix graph( N, vector<bool>(N) ) ;
  24.  
  25. // read and parse subsequent lines
  26. size_t i = 0 ;
  27. while( getline( cin, line ) )
  28. {
  29. vector<string> elements ;
  30. split( elements, line, is_any_of(", ") );
  31. assert( (i<N) && (vertices[i]==elements[0]) && (elements.size()==N+1) ) ;
  32. ++i ;
  33. for( size_t j=0 ; j<N ; ++j )
  34. graph[i][j] = elements[j+1] == "1" ;
  35. }
  36. assert( i == N ) ;
  37. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Re: creating dynamic array

 
0
  #8
Aug 27th, 2007
can u expain what this code signifies

split( elements, line, is_any_of(", ") );
assert( (i<N) && (vertices[i]==elements[0]) && (elements.size()==N+1) ) ; ++i ;
for( size_t j=0 ; j<N ; ++j )
graph[i][j] = elements[j+1] == "1" ;


Also how do i print the edge values..
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Re: creating dynamic array

 
0
  #9
Aug 27th, 2007
Also your code given code gives following compilation errors:-

:\srlgrp\boostlib\boost_1_34_0\boost\algorithm\string\yes_no_type.hpp(22) : error C2265: '<Unknown>' : reference to a zero-sized array is illegal
c:\srlgrp\boostlib\boost_1_34_0\boost\algorithm\string\yes_no_type.hpp(23) : see reference to class template instantiation 'boost::algorithm::size_descriptor<I>' being compiled
c:\srlgrp\boostlib\boost_1_34_0\boost\mpl\bool.hpp(37) : fatal error C1506: unrecoverable block scoping error
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: creating dynamic array

 
0
  #10
Aug 27th, 2007
>> can u expain what this code signifies
>> split( elements, line, is_any_of(", ") );
split() is used to split string line into parts separated by characters ',' or space. these parts are then put into the vector elements
>> assert( (i<N) && (vertices[i]==elements[0]) && (elements.size()==N+1) ) ;
verify that the input is sane
>> for( size_t j=0 ; j<N ; ++j )
graph[i][j] = elements[j+1] == "1" ;
if the element in the string part is "1", set the corrosponding element in the adjacency matrix to true.
>> Also your code given code gives following compilation errors
it does not for me; i'm using boost_1_34_1 (not boost_1_34_0), but i'm certain that boost_1_34_0 should be ok.

there were two errors in the earlier code; ++i (line 32) should me moved to the end of the while loop. also, getline( cin, line ) should be changed to getline( file, line ).
here is the modified code with a testcase and some diagnostic output.
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <boost/algorithm/string.hpp>
  6. #include <cassert>
  7. #include <algorithm>
  8. #include <iterator>
  9. using namespace std ;
  10. using namespace boost ;
  11.  
  12. typedef vector< vector<bool> > adjacency_matrix ;
  13.  
  14. int main()
  15. {
  16. vector<string> vertices ;
  17. ifstream file( "graph.txt" ) ;
  18. string line ;
  19.  
  20. // read and parse the first line
  21. assert( getline( file, line ) ) ;
  22. split( vertices, line, is_any_of(", ") );
  23.  
  24. const size_t N = vertices.size() ;
  25. adjacency_matrix graph( N, vector<bool>(N) ) ;
  26.  
  27. // read and parse subsequent lines
  28. size_t i = 0 ;
  29. while( getline( file, line ) )
  30. {
  31. vector<string> elements ;
  32. split( elements, line, is_any_of(", ") );
  33. assert( ( i<N ) && ( vertices[i]==elements[0] )
  34. && (elements.size()==N+1) ) ;
  35. for( size_t j=0 ; j<N ; ++j )
  36. graph[i][j] = elements[j+1] == "1" ;
  37. cout << line << " => " ;
  38. copy( graph[i].begin(), graph[i].end(),
  39. ostream_iterator<int>(cout," ") ) ;
  40. cout << '\n' ;
  41. ++i ;
  42. }
  43. assert( i == N ) ;
  44. }
  45.  
  46. /** graph.txt
  47. L1,L2,L3,L4,L5
  48. L1,0,0,0,1,0
  49. L2,1,0,0,0,1
  50. L3,1,0,0,0,0
  51. L4,0,1,1,0,1
  52. L5,0,0,1,1,0
  53. */
  54. /** output
  55. >g++ -Wall -std=c++98 -I/usr/local/include adj_matrix.cpp && ./a.out
  56. L1,0,0,0,1,0 => 0 0 0 1 0
  57. L2,1,0,0,0,1 => 1 0 0 0 1
  58. L3,1,0,0,0,0 => 1 0 0 0 0
  59. L4,0,1,1,0,1 => 0 1 1 0 1
  60. L5,0,0,1,1,0 => 0 0 1 1 0
  61. */
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