split() is used to split string line into parts separated by characters ',' or space. these parts are then put into the vector elements
if the element in the string part is "1", set the corrosponding element in the adjacency matrix to true.
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 ).
#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
*/