vector does all the dynmaic allocation for you -- just call its push_back() method to add a new row.
string line;
vector<string> array;
...
// now put this inside the file read loop just after getline()
array.push_back(line);
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Maybe a linked list of some sort may be a better idea?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
you can repesent a graph as an adjacency matrix. http://www.cs.usask.ca/resources/tutorials/csconcepts/1999_8/tutorial/beginner/matrices/matrix.html
using the specialization vector 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)
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <cassert>
using namespace std ;
using namespace boost ;
typedef vector< vector<bool> > adjacency_matrix ;
int main()
{
vector<string> vertices ;
ifstream file( "whatever.txt" ) ;
string line ;
// read and parse the first line
assert( getline( cin, line ) ) ;
split( vertices, line, is_any_of(", ") );
const size_t N = vertices.size() ;
adjacency_matrix graph( N, vector<bool>(N) ) ;
// read and parse subsequent lines
size_t i = 0 ;
while( getline( cin, line ) )
{
vector<string> elements ;
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" ;
}
assert( i == N ) ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287