954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

creating dynamic array

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

tonyaim83
Light Poster
49 posts since Aug 2007
Reputation Points: 8
Solved Threads: 0
 

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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

tonyaim83
Light Poster
49 posts since Aug 2007
Reputation Points: 8
Solved Threads: 0
 

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
 

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.

tonyaim83
Light Poster
49 posts since Aug 2007
Reputation Points: 8
Solved Threads: 0
 
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
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

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
 

can u expain what this code signifies

split( elements, line, is_any_of(", ") );
assert( (i

tonyaim83
Light Poster
49 posts since Aug 2007
Reputation Points: 8
Solved Threads: 0
 

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: '' : 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' being compiled
c:\srlgrp\boostlib\boost_1_34_0\boost\mpl\bool.hpp(37) : fatal error C1506: unrecoverable block scoping error

tonyaim83
Light Poster
49 posts since Aug 2007
Reputation Points: 8
Solved Threads: 0
 
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You