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..

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);

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.

Member Avatar for iamthwee

Maybe a linked list of some sort may be a better idea?

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.

How do i create a 2-D array using a vector which is dynamic in nature.

You can make use of vector<vector<T> >

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<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)

#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 ) ;
}

can u expain what this code signifies

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


Also how do i print the edge values..

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

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.

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <cassert>
#include <algorithm>
#include <iterator>
using namespace std ;
using namespace boost ;

typedef vector< vector<bool> > adjacency_matrix ;

int main()
{
  vector<string> vertices ;
  ifstream file( "graph.txt" ) ;
  string line ;

  // read and parse the first line
  assert( getline( file, 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( file, line ) )
  {
    vector<string> elements ;
    split( elements, line, is_any_of(", ") );
    assert( ( i<N ) && ( vertices[i]==elements[0] ) 
                      && (elements.size()==N+1) ) ;
    for( size_t j=0 ; j<N ; ++j )
      graph[i][j] = elements[j+1] == "1" ;
    cout << line << " => " ;
    copy( graph[i].begin(), graph[i].end(), 
               ostream_iterator<int>(cout," ") ) ;
    cout << '\n' ;
    ++i ;
  }
  assert( i == N ) ;
}

/** graph.txt
L1,L2,L3,L4,L5
L1,0,0,0,1,0
L2,1,0,0,0,1
L3,1,0,0,0,0
L4,0,1,1,0,1
L5,0,0,1,1,0
*/
/** output
>g++ -Wall -std=c++98 -I/usr/local/include adj_matrix.cpp && ./a.out
L1,0,0,0,1,0 => 0 0 0 1 0
L2,1,0,0,0,1 => 1 0 0 0 1
L3,1,0,0,0,0 => 1 0 0 0 0
L4,0,1,1,0,1 => 0 1 1 0 1
L5,0,0,1,1,0 => 0 0 1 1 0
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.