944,114 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4556
  • C++ RSS
Aug 24th, 2007
0

creating dynamic array

Expand Post »
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..
Similar Threads
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Aug 24th, 2007
0

Re: creating dynamic array

vector does all the dynmaic allocation for you -- just call its push_back() method to add a new row.
C++ Syntax (Toggle Plain Text)
  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);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Aug 27th, 2007
0

Re: creating dynamic array

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.
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Aug 27th, 2007
0

Re: creating dynamic array

Maybe a linked list of some sort may be a better idea?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 27th, 2007
0

Re: creating dynamic array

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.
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Aug 27th, 2007
0

Re: creating dynamic array

Click to Expand / Collapse  Quote originally posted by tonyaim83 ...
How do i create a 2-D array using a vector which is dynamic in nature.
You can make use of vector<vector<T> >
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 27th, 2007
0

Re: creating dynamic array

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)
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Aug 27th, 2007
0

Re: creating dynamic array

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..
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Aug 27th, 2007
0

Re: creating dynamic array

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
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Aug 27th, 2007
0

Re: creating dynamic array

>> 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.
C++ Syntax (Toggle Plain Text)
  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. */
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dictionary Project
Next Thread in C++ Forum Timeline: Why atof causes a C0000005 exeption?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC