| | |
creating dynamic array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2007
Posts: 49
Reputation:
Solved Threads: 0
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..
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.
C++ Syntax (Toggle Plain Text)
string line; vector<string> array; ... // now put this inside the file read loop just after getline() array.push_back(line);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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)
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)
#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 ) ; }
•
•
Join Date: Aug 2007
Posts: 49
Reputation:
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: '<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
:\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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
>> 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.
>> 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)
#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 */
![]() |
Similar Threads
- get length of a dynamic array (C++)
- Dynamic Array of Structures, Loop problem! (C++)
- Creating dynamic array structures (C++)
- dynamic array of structures problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Dictionary Project
- Next Thread: Why atof causes a C0000005 exeption?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






